i have a variable with 7 bits like 1000110. i want to extract the First 4 bits like 1000 from my variable and put it in another variable
and extract also the last 3 bits like 110.
i don't know how to do this
can somebody help me for this problem.
a = 1000110
var1 = (a & 0x78) >> 3;
var2 = (a & 0x7);
generally you need to mask with all the bits on (0x78 = 0111 1000) that youwant to keep and then shift to the right by the number of bits you want to eliminate (in this case 3). It is more common to split into 4 bits so mask is 0xf for one var and 0xf0 and then shift by 4 for the other (or just shift by 4).
In the case of a byte variable (which is an 8-bit unsigned type) shifting right by four is
enough, a masking operation is not needed because there are only 8 bits and unsigned
shift right feeds in zeros (no so for a signed shift).