0
Decimal to binary using simple coding in c language?
3 Réponses
+ 1
Look at this
https://www.sololearn.com/learn/4401/?ref=app
And I also copied this from somewhere
https://code.sololearn.com/cSjz5VxhSsp8/?ref=app
+ 2
I'm no expert but the way I did it was to use bitwise '&' with a 1 to get the value (state) of the least significant bit of the number, then start filling an int array, then, just continue (using some sort of loop) whilst doing a right shift until the number is 0, then output the array in reverse order.
(only works for a positive integer though)
One thing I don't like about my method was the fact that it's in an int array, What I might try (tomorrow) is to fill a char array using a switch/case construct , reverse it then append a null '\0' and output it as a string.
0
Here is a slightly less mathy way, by using a lookup table. Reformat the decimal number into a hexadecimal or octal string by using sprintf. (Too bad, sprintf won't format directly to binary). Then, digit by digit, from a lookup table print the binary bit pattern that defines that digit.
Here is an implementation that uses octal, which conveniently has only 8 digits to look up:
https://code.sololearn.com/c1WcNAGuZrrg/?ref=app