Skip to content

Instantly share code, notes, and snippets.

@trickre
Forked from felipewer/print_char_bits.c
Last active December 15, 2017 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trickre/5c6a32846675f965cfb4101532eceeb2 to your computer and use it in GitHub Desktop.
Save trickre/5c6a32846675f965cfb4101532eceeb2 to your computer and use it in GitHub Desktop.
Print bits of unsigned char
//This program enable you to see bit of variable(char and int).
#include <stdio.h>
void printbits(unsigned char v) {
int i; // for C89 compatability
for(i = 7; i >= 0; i--) putchar('0' + ((v >> i) & 1));
}
void printbits_int(int v) {//for 32bit variable
int i,j;
for(i=8*sizeof(int)-1; i >= 0; ) {
for(j=4; j>0; j--) {
putchar('0' + ((v >> i) & 1));
i--;
}
putchar(' ');
}
}
void main(){
printf("This program enable you to see bit of variable.\n");
printf("Ener one character you want see its bit(= ascii code).\n");
char x=getchar();
printbits(x);
printf("\nEner variable(int) you want see its bit.\n");
int y=0;
scanf("%d",&y);
printbits_int(y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment