Mega Code Archive

 
Categories / C / Development
 

Test bits

#include <stdio.h> int main(void) { char byte = 67; int bitcount = 8; /* supposed bits in a byte */ printf("char byte = 67 or binary "); for(; bitcount > 0; bitcount--) { /* if the high order bit is set [left one], print `1' */ printf("%d", (byte & 128) ? 1 : 0 ); byte <<= 1; /* push all bits to the left */ } printf("\n"); return 0; }