Skip to content

Instantly share code, notes, and snippets.

@samueltcsantos
Last active May 1, 2017 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samueltcsantos/52d9864b8619fd48b3a2368aaab040a4 to your computer and use it in GitHub Desktop.
Save samueltcsantos/52d9864b8619fd48b3a2368aaab040a4 to your computer and use it in GitHub Desktop.
Convert decimal to binary using Big-Endian notation.
#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