jweinst1
8/31/2018 - 12:13 AM

practice with getting left most bit in c

practice with getting left most bit in c

#include <stdio.h>
#include <limits.h>
// Bit shifting practice in C

void showbits(unsigned int x) {
    for(int i = (sizeof(int) * 8) - 1; i >= 0; i--) {
       (x & (1u << i)) ? putchar('1') : putchar('0');
    }
    printf("\n");
}

void show_binary_bools(unsigned int x) {
  for(int i = (sizeof(int) * 8) - 1; i >= 0; i--) {
    printf("This is raw shift %u\n", x & (1u << i));
    printf("This is boolean result %d\n", (x & (1u << i)) ? 1 : 0);
  }
}

int main(void) {
  printf("Char bit size is %u\n", CHAR_BIT);
  showbits(500);
  show_binary_bools(500);
  return 0;
}