How to get the 4 least significant bits in a byte with C

1 Answer

0 votes
#include <stdio.h>
#include <stdint.h>

void print_bits(uint8_t x) {
    for (int i = 7; i >= 0; i--)
        putchar((x & (1 << i)) ? '1' : '0');
}

int main(void) {
    uint8_t byteValue = 0b11010110;
    uint8_t lower4 = byteValue & 0x0F;   // keep only the 4 LSBs

    printf("byteValue = %u\n", byteValue);
    printf("lower4    = %u\n", lower4);
    
    print_bits(byteValue);
    putchar('\n');
    print_bits(lower4);
    putchar('\n');

    return 0;
}

  
  
/*
run:
  
byteValue = 214
lower4    = 6
11010110
00000110
  
*/

 



answered Dec 27, 2025 by avibootz
...