c - Byte-wise output of uint32_t -
i trying understand bitwise operators better. have number of type uint32_t
trying output byte byte. code 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 above code sample 9. byte array should stored in memory so: 09 00 00 00
(in increasing order of addresses). if so, output should be: byte 1 = 09
byte 2 = 00
byte 3 = 00
byte 4 = 00
output is:
byte 1 = 0 byte 2 = 0 byte 3 = 0 byte 4 = 9
i on scheme little-endian obtained so:
int is_bigendian() { int = 1; char *low = (char *) (&i); homecoming *low ? 0 : 1; }
is behaviour correct? why seeing behaviour?
remember, both of operands same endianness.
on little endian, yes, 9 stored 0x09000000. masking 0xff000000, stored in memory 0x000000ff hence used in pattern mask.
if want see effect fully, ali veli says , iterate on memory byte-by-byte using char
pointer.
c endianness bit-shift little-endian
No comments:
Post a Comment