A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • one year ago

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

  • This Question is Closed
  1. Lyrae
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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!

  2. rsmith6559
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  3. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  4. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  5. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.