For example, I have two digit bits:
0b0111111
0b0000110
I want to shift a state variable by 7 digits and combine them together.
0b00001100111111
Can I accomplish by shifting?
For example, I have two digit bits:
0b0111111
0b0000110
I want to shift a state variable by 7 digits and combine them together.
0b00001100111111
Can I accomplish by shifting?
You do this by shifting the bottom number left 7 digits, then performing a bitwise OR of the result and the first number.
unsigned int a = 0x3f;
unsigned int b = 0x06;
unsigned int result = (b << 7) | a;
unsigned char a = 0b00000110;
unsigned char b = 0b01111111;
unsigned short c = (b << 8); // shift everything left 8 bits to set the high bits
c &= 0xFF00; // clear out the lower bits - not necessary in C
c |= a; // set the lower 8 bits
unsigned int X = 0b00111111;
unsigned int Y = 0b00000110;
unsigned int Z = ((X << 7) & 0xFF00) | Y;
int a = 0b0111111;
int b = 0b0000110;
int combined = (a << 7) | b;