Skip to content

Instantly share code, notes, and snippets.

@odedlaz
Created May 4, 2017 14:58
Show Gist options
  • Save odedlaz/bb5ea391f6d615775c60a156f2210dac to your computer and use it in GitHub Desktop.
Save odedlaz/bb5ea391f6d615775c60a156f2210dac to your computer and use it in GitHub Desktop.
Represent an integer in binary format
#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");
}
@odedlaz
Copy link
Author

odedlaz commented May 4, 2017

11111111111111111111111111110110 (x: -10)
00000000000000000000000000001010 (-x: 10)
11111111111111111111111111111111 (-1: -1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment