Juuggo
5/12/2017 - 7:29 AM

display a hex in bits; use bitwise AND with mask.

display a hex in bits; use bitwise AND with mask.

#include <stdio.h>
#include <limits.h>
void displayBits( unsigned value );

void displayBits( unsigned value ){
  unsigned counter = 0;
  unsigned mask = 1 << CHAR_BIT * sizeof(char) - 1;

  for ( ; counter <= CHAR_BIT; counter++ ) {
    putchar(mask & value? '1' : '0');
    value <<= 1;

    if ( counter % 8 == 0 ) {
      putchar(' ');
    }
  }
}