Created
May 4, 2017 14:58
-
-
Save odedlaz/bb5ea391f6d615775c60a156f2210dac to your computer and use it in GitHub Desktop.
Represent an integer in binary format
This file contains hidden or 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> | |
void dtob(unsigned int word, char* text) { | |
for (int bit = 31; bit >= 0; bit--) { | |
int shift = word >> bit; | |
printf("%d", shift & 1); | |
} | |
printf(" (%s: %d)\n", text, word); | |
} | |
int main(int argc, char *argv[]) { | |
// gcc -o tester tester.c <num> | |
int x = 5; | |
if (argc == 2) { | |
x = atoi(argv[1]); | |
} | |
dtob(x, "x"); | |
dtob(~x + 1, "-x"); | |
dtob(-1, "-1"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
11111111111111111111111111110110 (x: -10)
00000000000000000000000000001010 (-x: 10)
11111111111111111111111111111111 (-1: -1)