Bit concatenation in C++
-
hi guys.. I have written the below code in C. I have 3 bits in three different integer variables. How can I concatenate those values assuring that i need only binary values (1/0).
#include <stdio.h>
#include "manojturbo5.h"
#include "bitsconv.h"
#include "manojencoder2.h"void main()
{
int message,interleaver,u,p,q,r,final,result,i;printf("Enter the message\n");
scanf("%d",&message);bitconv (message);
for(r=0;r<32;r++)
{
u = array[31-r];
p = encoder(array[31-r]);
q = encoder2(array[31-r]);printf("%d\t %d\t %d\n",u,p,q);
}
return;
} -
hi guys.. I have written the below code in C. I have 3 bits in three different integer variables. How can I concatenate those values assuring that i need only binary values (1/0).
#include <stdio.h>
#include "manojturbo5.h"
#include "bitsconv.h"
#include "manojencoder2.h"void main()
{
int message,interleaver,u,p,q,r,final,result,i;printf("Enter the message\n");
scanf("%d",&message);bitconv (message);
for(r=0;r<32;r++)
{
u = array[31-r];
p = encoder(array[31-r]);
q = encoder2(array[31-r]);printf("%d\t %d\t %d\n",u,p,q);
}
return;
} -
-
hi guys.. I have written the below code in C. I have 3 bits in three different integer variables. How can I concatenate those values assuring that i need only binary values (1/0).
#include <stdio.h>
#include "manojturbo5.h"
#include "bitsconv.h"
#include "manojencoder2.h"void main()
{
int message,interleaver,u,p,q,r,final,result,i;printf("Enter the message\n");
scanf("%d",&message);bitconv (message);
for(r=0;r<32;r++)
{
u = array[31-r];
p = encoder(array[31-r]);
q = encoder2(array[31-r]);printf("%d\t %d\t %d\n",u,p,q);
}
return;
}I was reading the previous thread and I think the problem you are having is understanding the difference between storing the values and using the values. Richard showed you how to pack the three binary values into a single integer:
int Answer = (u << 2) | (p << 1) | q
That is for storing them. Don't worry about the actual value of Answer. When you need to use the binary values, you have to unpack them. There are different ways to do it, but I think this method is the easiest way for you to understand it:
q = Answer & 1;
p = (Answer >> 1) & 1;
u = (Answer >> 2) & 1;Soren Madsen
"When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty
-
hi guys.. I have written the below code in C. I have 3 bits in three different integer variables. How can I concatenate those values assuring that i need only binary values (1/0).
#include <stdio.h>
#include "manojturbo5.h"
#include "bitsconv.h"
#include "manojencoder2.h"void main()
{
int message,interleaver,u,p,q,r,final,result,i;printf("Enter the message\n");
scanf("%d",&message);bitconv (message);
for(r=0;r<32;r++)
{
u = array[31-r];
p = encoder(array[31-r]);
q = encoder2(array[31-r]);printf("%d\t %d\t %d\n",u,p,q);
}
return;
} -
I cannot use shifting operator because it will give numbers other than 1/0. If I shift it once i will get 2. I dont want that. I need only binary (1/0).
Manoj7390 wrote:
I cannot use shifting operator because it will give numbers other than 1/0.
that makes no sense at all.
Manoj7390 wrote:
If I shift it once i will get 2.
1 = 00000001 (in binary) 1 << 1 = 2 2 = 00000010 (in binary) you might benefit from reading about binary math and how computers store numbers.