since I'm fairly new to C, so this might be a dumb question. I aimed to use the bitwise operators & to do bit masking. For example: If inputs are 0x00F7 and 0x000F,my program should returns 0x0007. However, the output of 0x007&0x000F is just 7, 0x0077&0x00FF is just 77. So is there any way I can count the characters of a hexadecimal number so that I can know how many zeros should I print out?
printf("%X",0x077&0x00FF);
CodePudding user response:
You don't need to count.
You can get printf
to pad the number for you, just specify the maximum length like so:
printf("X", 0x77 & 0xff);
Note that when you write an integer (no quotes) it makes no difference if you write 0x0077 or 0x77.
Only when you use printf
to output a string zeros can be added for visibility.
CodePudding user response:
printf("%0*X",sizeof((unsigned short)(0x077&0x00FF))*2, 0x077&0x00FF);