I tried:
#include<stdio.h>
main()
{
int a=10000;
unsigned char cp1=0,cp2=0,cp3=0,cp4=0;
cp1 = (a & 0xff000000) >> 24;
cp2 = (a & 0x00ff0000) >> 16;
cp3 = (a & 0x0000ff00) >> 8;
cp4 = (a & 0x000000ff) ;
printf("%d %d %d %d\n",cp1,cp2,cp3,cp4);
}
My output is:
0 0 39 16
I found (39<<8) + 16=10000.
I could not understand cp3=(a & 0x0000ff00)>>8; ==39 how it is working?
I know 0xff=255, I want to know how the (&) operation and 0xff are working together and taking particular bits.
Can you teach me how it is working?
unsigned.unsigned charcan be represented by asigned int(which is usually the case), the arguments will be promoted tosigned intwhich matches the conversion specifiers.