Last active
August 29, 2015 14:17
-
-
Save duyet/3b34e2d3d351682b293c to your computer and use it in GitHub Desktop.
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
// Van-Duyet Le <[email protected]> | |
// Home: http://lvduit.com | |
#include <stdio.h> | |
long dec2bin(int n) { | |
long result = 0; | |
for (int c = 31; c >= 0; c--) { | |
int k = n >> c; | |
if (k & 1) | |
result = result * 10 + 1; | |
else | |
result *= 10; | |
} | |
return result; | |
} | |
// Test it | |
int main() { | |
printf("%ld\n", dec2bin(3)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment