Mega Code Archive

 
Categories / C / Code Snippets
 

Shift bitwise and display the result

#include <stdio.h> void show_binary(unsigned j); int main(void) { unsigned short j; j = 45678; show_binary(j); j = j << 1; show_binary(j); j = j >> 1; show_binary(j); return 0; } void show_binary(unsigned j) { int x; for( x = 32768; x > 0; x = x / 2 ) if(j & x) printf("1 "); else printf("0 "); printf("\n"); }