anonymous
  • anonymous
Question for a C program to convert a decimal number to a binary number
Computer Science
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
schrodinger
  • schrodinger
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
Lyrae
  • 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!
rsmith6559
  • 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.
anonymous
  • 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

Looking for something else?

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

More answers

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

Looking for something else?

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