Skip to content

Instantly share code, notes, and snippets.

@siddMahen
Created October 11, 2011 21:32
Show Gist options
  • Save siddMahen/1279519 to your computer and use it in GitHub Desktop.
Save siddMahen/1279519 to your computer and use it in GitHub Desktop.
Printing Bits
CC = gcc
CFLAGS = -I.
test: main.o
$(CC) -o $@ $(CFLAGS) main.o
main.o: main.c
$(CC) -c main.c $(CFLAGS)
#include<stdio.h>
#include<stdlib.h>
#define BITS_PER_BYTE 8
int main(int argv, char *argc[])
{
if(!argc[1]){
printf("Positive integer input required.\n");
exit(1);
}
const int SHIFT = BITS_PER_BYTE * sizeof(unsigned) - 1;
unsigned value = atoi(argc[1]);
const unsigned MASK = 1 << SHIFT;
int i;
for(i = 1; i <= SHIFT + 1; i++)
{
printf("%c",( value & MASK ? '1' : '0' ));
value <<= 1;
if ( i % 8 == 0 )
printf(" ");
}
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment