if u16 data type contains 0x1f then does this means its 0x1f00? so 8 bits 1f is in upper half of 16 bit field

I have u16 field in a code that assigned value like 0x1f does this means 0x1f00? can any one please tell me this. thanks. so 1f in upper half 8 bits of 16 bit field.

like 1f00
or 0001111100000000

>Solution :

No, 0x1f is just another way of writing 31, so it does NOT mean 0x1f00, which is another way of writing 7936. (Tip: type "0x1f00 in decimal" into Google to verify this for yourself.)

It is correct to say that 0x1f is the same as 0x001f.

There is no standard data type named u16, but I suppose you might be talking about uint16_t from stdint.h. The C standard does not say exactly how a uint16_t is encoded as bits on your target machine, but most computers use a convention called "little endian", which means the number will be stored as two bytes, and the least-significant byte is first. So a uint16_t with a value of 0x1f (also known as 0x001f) would be encoded as two bytes, in this order: 0x1f 0x00.

You can experiment with this for yourself by making a struct with some uint16_t members in them, setting them to different values, and then casting a pointer to that struct to type uint8_t *. Then use the pointer to read the byte values at different locations in the struct, to see exactly how your struct is stored in memory.

Leave a Reply