A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 one year ago
Question for a C program to convert a decimal number to a binary number
anonymous
 one year ago
Question for a C program to convert a decimal number to a binary number

This Question is Closed

Lyrae
 one year ago
Best ResponseYou've already chosen the best response.0All 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 Nbits (including zero) and mask (ANDtype 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!

rsmith6559
 one year ago
Best ResponseYou've already chosen the best response.0You may want to check out: https://www.khanacademy.org/math/prealgebra/applyingmathreasoningtopic/alternatenumberbases/v/numbersystemsintroduction to see how to convert between different number bases.

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0int 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

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0For 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.
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.