Created
October 11, 2011 21:32
-
-
Save siddMahen/1279519 to your computer and use it in GitHub Desktop.
Printing Bits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CC = gcc | |
CFLAGS = -I. | |
test: main.o | |
$(CC) -o $@ $(CFLAGS) main.o | |
main.o: main.c | |
$(CC) -c main.c $(CFLAGS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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