Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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