# Conversion from decimal to bianary

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 01-27-2012
Conversion from decimal to bianary
This a program for conversion from decimal to binary where I have some problem to understand the binary function---

Code:

``` #include <iostream> using namespace std; void binary(int); int main(void) {         int number;         cout << "Please enter a positive integer: ";         cin >> number;         if (number < 0)                 cout << "That is not a positive integer.\n";         else {                 cout << number << " converted to binary is: ";                 binary(number);                 cout << endl;         } } void binary(int number) {         int remainder;         if(number <= 1) {                 cout << number;                 return;         }         remainder = number%2;         binary(number >> 1);         cout << remainder; }```

Say, I would like to convert Decimal 11 to binary number. The program is running properly and giving the answer 1011. I guess that the binary function will be started with 11 and goes to

remainder = number%2;

The reminder will be 1 but the number is still 11 , right ? I did not understand the function here : binary(number >> 1) due to the ">>" sign.

Anyone explain it for me a little ?
• 01-27-2012
whiteflags
Well this function uses recursion to repeat the code until it has displayed all of the bits. The call binary(number >> 1); is where the recursion takes place, and binary's argument provides the new number to operate on and display. The code number >> 1 is a bitwise operation. If you can imagine a series of bits, each shift by one removes the rightmost bit.
• 01-27-2012
Excuse me, but I don't know what is bitwise operation. binary(number >> 1) here the number is 11, right ? Then ,is it goes top of the program --> void binary(int number) ?
• 01-27-2012
bernt
• 01-28-2012
Thanks bernt :) I will have a look and write again.
• 01-28-2012
Elysia
• 01-29-2012
Sorry for late reply. I need to do something yesterday so was not able to write back. I got the term ---> (number >> 1)

Shifting left by n bits on a signed or unsigned binary number has the effect of multiplying it by 2n. Shifting right by n bits on an unsigned binary number has the effect of dividing it by 2n (rounding towards 0)

For the number 11, its goes to the function first with 5 then, 2 and 1. My question is then it should print the binary as 1101, right ? Also, after the term remainder = number%2, every-time due to recursion its again go in the top to use the function. When the
cout << remainder;

It would help me if someone have time to explain me this. Thanks and wishes from Munich :)
• 01-29-2012
Another question, what is the data type of binary numbers ?

Say, the decimal number 167 has data type int. Whats for 0 or 1 or something more say, 1100101 ?
• 01-29-2012
Elysia
There is no type for binary numbers since memory is byte-addressed. Hence, the smallest unit the processor works with is a byte (ie char).
You can only treat existing data types as binary by using bit operations.

Well, perhaps I should expand upon that. All data types are in some way or form binary since all data processed by a computer is a number. A character is merely a number that is interpreted differently when printed to screen.
Hence, all data has a binary representation, but there is no data type where you can directly store 0s and 1s in C++.
• 01-29-2012
Thanks Elysia :) I had another question that is more importation for me to understand the program. Could you please explain me the binary() function to me properly ?
• 01-29-2012
Elysia
Nope, it's unintuitive. Taking the reminder may work in math, but in computer science, there is a way better and intuitive way.

EDIT: Here's how you do it with computers:
Code:

```#include <iostream> int main() {         unsigned int num = 1234567890;         int shift = 32;         while (shift-- > 0)                 std::cout << ((num >> shift) & 0x1); }```
• 01-29-2012
Sorry but I did not understand this code. It seems that if I put a decimal as number then it returns the binary with zeros before. In total 32 digits are printed. However, thats the meaning of shift-- > 0. Also, I did not understand the form: std::cout << ((num >> shift) & 0x1); specially why do you need to put std infornt.

I would rather prefer the explanation of the function binary () in the code posted before.
• 01-29-2012
Elysia
It assumes a number is 4 bytes, which means it has 32 bits. Therefore, it prints all those 32 bits. You can get rid of the leading 0s if you want.
• 01-29-2012
Something about the code I posted before ? :)
Quote:

For the number 11, its goes to the function first with 5 then, 2 and 1. My question is then it should print the binary as 1101, right ? Also, after the term remainder = number%2, every-time due to recursion its again go in the top to use the function. When the
cout << remainder;
• 01-29-2012
whiteflags
Quote:

For the number 11, its goes to the function first with 5 then, 2 and 1. My question is then it should print the binary as 1101, right ? Also, after the term remainder = number%2, every-time due to recursion its again go in the top to use the function. When the
cout << remainder;

It would help me if someone have time to explain me this. Thanks and wishes from Munich :)

Let's discuss the recursion: like you noted, the number 11 would be divided, first resulting in 5 then 2, then 1. The computer will follow the calls of binary in order.

binary( 11 );
binary( 5 );
binary( 2 );
binary( 1 );

When the calls return, the stack is unwound in reverse order, but in this case it doesn't affect the outcome.

The remainder is a way of finding the highest bit for the current number, and by shifting the number left, you work with the next highest bit and so forth.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last