Last active
May 1, 2017 20:00
-
-
Save samueltcsantos/52d9864b8619fd48b3a2368aaab040a4 to your computer and use it in GitHub Desktop.
Convert decimal to binary using Big-Endian notation.
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> | |
#include <string.h> | |
#include <math.h> | |
#define BIT_0 "0" | |
#define BIT_1 "1" | |
int main(int argc , char **argv){ | |
int number = atoi(argv[1]); | |
char bits[64] = ""; // string array to represent the binary number | |
char r=0; // the rest can only be 0 or 1 | |
int q=1; // the quotient | |
while (q > 0){ | |
q = number / 2; | |
r = number % 2; | |
(r == 0) ? strcat(bits, BIT_0) : strcat(bits, BIT_1); | |
printf("n = %d , q = %d, r = %d \n", number, q, r); | |
number = q; | |
} | |
printf("Binary 0b%s \n", bits); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment