In: Computer Science
I am trying to create bit masks to create a new binary number with the original bits' orders turned around, and concataneted.
For example,
int test=0b01100010001010001001(20 bits)
now, I need bit masking to make it:
int result = 20th bit, 10-1 bits, 11th bit, 19-12 bits, in this order.
So in this case, answer would be:
0(20th bit)10100010(10-1 bits)0(11th bit)1011000100(19-12 bits).
Im trying to do something like
int res |= test & 0x7F800;
res |= test& 0x400;
Can't seem to be able to figure out the rest.
Thanks
Greetings!!
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int test=0b01100010001010001001;
int temp,copy;
printf("given number %x \n \n",test);
temp=test;
copy=test;
temp=test & 0x003ff; //extract 10-1
printf("extracted bits from 10-1 %x \n \n",temp);
temp=temp<<9; //shift to 19-10
printf("shifted to 19-10 %x \n \n",temp);
test=temp & 0x7fe00; //append
printf("appended to 19-10 %x \n \n",test);
temp=copy & 0x00400; //extract 11th bit
printf("extracted bit from 11 %x \n \n",temp);
temp=temp>>2;
printf("shifted to bit 9 %x \n \n",temp);
test=temp | test; //appended 11th bit
printf("appended to 9 %x \n \n",test);
temp=copy & 0x7f800; //extract 19-12 bit
printf("extracted bits from 19-12 %x \n \n",temp);
temp=temp>>11;
printf("shifted to 8-1 %x \n \n",temp);
test=temp | test;
printf("appended to 8-1 %x \n \n",test);
return 0;
}
Output screenshots:
Hope this helps