Skip to content

Instantly share code, notes, and snippets.

@nissuk
Created April 27, 2012 10:30
Show Gist options
  • Save nissuk/2508233 to your computer and use it in GitHub Desktop.
Save nissuk/2508233 to your computer and use it in GitHub Desktop.
C: 整数を2進数に変換して表示!
#include <stdio.h>
void main() {
int input;
int bit;
int show;
int i;
while (1) {
printf(">> ");
scanf("%d", &input);
show = 0;
for (i = sizeof(int) * 8 - 1; i > -1; i--) {
bit = (input & (1 << i)) >> i;
if (!show && bit == 1) show = 1;
if (show) printf("%d", bit);
}
puts("");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment