Ask your own question, for FREE!
Computer Science 12 Online
OpenStudy (anonymous):

Question for a C program to convert a decimal number to a binary number

OpenStudy (lyrae):

All numbers are stored as binary numbers in a computer; how they're displayed to the user depends on context. A fairly simple way to get the binary representation is to perform a right shift of N-bits (including zero) and mask (AND-type mask, size should match decimal type size) out the first bit. The resulting decimal number will be equal to the Nth bit and you use a loop to get the entire binary number. Note that you should go from high to low bits if you are appending (ie. printing, putting in a string, etc) otherwise the result will be reversed. Nth 0 xxxxxxxxxxx ^ | This first if appending!

OpenStudy (rsmith6559):

You may want to check out: https://www.khanacademy.org/math/pre-algebra/applying-math-reasoning-topic/alternate-number-bases/v/number-systems-introduction to see how to convert between different number bases.

OpenStudy (anonymous):

int decimal = 200, x[10]; //convert dec to bin for(int i = 0; i<10; i++){ x[i] = decimal%2; //take mod 2 to get remainder decimal = decimal /2; divide by 2 } //print bin number for(int i = 9; i>=0; i--){ printf("%d" x[i]); } I didn't run it, but it should work, so let me know if it didn't work

OpenStudy (anonymous):

For small enough numbers, you can use `atoi()` to turn a decimal string into an `int` type. Then you can use bit wise operations like `&` and `<<` to get each bit from the `int` and put them into the binary string.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!