cbit-manipulation

Extracting the first 3 bits of an uint8


A struct I'm dealing with has a field defined as uint8_t. The spec also states that all multiple-byte fields are represented in host-endian format.

Bits 0:3 contain the information that I need (also an unsigned integer). Using plain C, how do I extract those 3 bits and convert it to a number type?


Solution

  • unit8_t a;
    unit8_t b;
    
    a = input data;
    
    b = a & 0x0F;       // b contains a number from 0 to 15