Get lowest bytes from an uint16_t in C

1.2k Views Asked by At

I want to get the lowest bytes of an uint16_t in C.

Example:

20544 = 0x5040
0x40 = 64

I tried, (X & ((1<<2) - 1)). This doesn't work for me.

1

There are 1 best solutions below

0
Govind Parmar On BEST ANSWER

You use bytes (plural), but a uint16_t is composed of two bytes, so I'm assuming you mean the least significant byte (singular). If that's so, here is one way of obtaining it:

uint8_t lsb = ((uint8_t)(((uint32_t)(val)) & 0xFF))