I am trying to understand bitwise operators better. I have a number of type uint32_t
which I am trying to output byte by byte. The code that does that is:
void printByteWise(uint32_t num) {
printf("byte 1 = %u\n", (num & 0xFF000000));
printf("byte 2 = %u\n", (num & 0x00FF0000));
printf("byte 3 = %u\n", (num & 0x0000FF00));
printf("byte 4 = %u\n", (num & 0x000000FF));
}
Say num
in the above code sample is 9. Then the byte array should be stored in memory like so:
09 00 00 00
(in increasing order of addresses). If so, the output should be:
byte 1 = 09
byte 2 = 00
byte 3 = 00
byte 4 = 00
but the output I get is:
byte 1 = 0
byte 2 = 0
byte 3 = 0
byte 4 = 9
I am on a system that is little-endian which is obtained like so:
int is_bigEndian() {
int i = 1;
char *low = (char *) (&i);
return *low ? 0 : 1;
}
Is this behaviour correct? Why am I seeing this behaviour?