Search code examples
cbitlsb

Get lowest bytes from an uint16_t in C


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.


Solution

  • 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))