Thread: Conversion from decimal to bianary

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    73

    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 ?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    73
    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) ?

  4. #4
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    The diagram on this page might help.
    Consider this post signed

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    73
    Thanks bernt I will have a look and write again.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    73
    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

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    73
    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 ?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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++.
    Last edited by Elysia; 01-29-2012 at 04:39 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    73
    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 ?

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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);
    }
    Last edited by Elysia; 01-29-2012 at 05:24 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    73
    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.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    std:: is part of namespaces. Read up about them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    73
    Something about the code I posted before ?
    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;

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by chaklader View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary to decimal conversion
    By caysonmars in forum C Programming
    Replies: 9
    Last Post: 06-17-2011, 10:24 PM
  2. decimal to binary conversion
    By roelof in forum C Programming
    Replies: 41
    Last Post: 05-16-2011, 03:32 AM
  3. Decimal to Hex Conversion
    By millsy2000 in forum C Programming
    Replies: 7
    Last Post: 08-10-2010, 01:04 PM
  4. conversion of char to decimal
    By mapleleafblue in forum C Programming
    Replies: 4
    Last Post: 01-28-2009, 09:37 AM
  5. how to do conversion decimal to hexidecimal
    By twocool82 in forum C Programming
    Replies: 7
    Last Post: 07-08-2007, 06:20 PM