CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2005
    Posts
    37

    My program works good but there are two things I don't understand yet

    I'm working with a program that converts an integer to it's binary equivalent. My program works fine. Now to get into some details: If I enter the integer 148 my program will output 10010100 which is what I want. I want it to output all eight bit places. But if I enter the integer 58 it only outputs this: 111010. It omits the leading two bits which are 00. So I want it always to output all eight bit places (00111010). I don't know what to add.

    And also the last loop in the function is written so variable j gets set to whatever i is minus one. I don't get the why of that.

    Here is what I got:
    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    void convertToBinary(int n);
    
    int main()
    {
    	int x;
    
    	cout << "Enter an integer: ";
    	cin >> x;
    
    	convertToBinary(x);
    
    	getch();
    	return 0;
    }
    
    void convertToBinary(int n)
    {
    	int binaryNumber[8];
    	int i = 0;
    
    	while(n > 0)
    	{
    		binaryNumber[i] = n % 2;
    		n /= 2;
    		i++;
    	}
    
    	cout << "The binary equivalent is ";
    
    	for(int j = i - 1; j >= 0; j--)
    		cout << binaryNumber[j] << " ";
    
    }
    Last edited by 357mag; September 8th, 2019 at 03:56 PM.

  2. #2
    Join Date
    Mar 2005
    Posts
    37

    Re: My program works good but there are two things I don't understand yet

    How do I use these code tags so my code looks correct? Okay now.
    Last edited by 357mag; September 8th, 2019 at 03:56 PM.

  3. #3
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: My program works good but there are two things I don't understand yet

    And also the last loop in the function is written so variable j gets set to whatever i is minus one. I don't get the why of that.
    That is because it only outputs those binary digits that have been converted - which is why 58 is output missing the leading 2 digits. The reason for this is that binaryNumber is not initialised to all 0 first. Consider:

    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    void convertToBinary(int n);
    
    int main()
    {
    	int x;
    
    	cout << "Enter an integer: ";
    	cin >> x;
    
    	convertToBinary(x);
    
    	getch();
    	return 0;
    }
    
    void convertToBinary(int n)
    {
    	int binaryNumber[8] = {0};
    	int i = 0;
    
    	while (n > 0)
    	{
    		binaryNumber[i] = n % 2;
    		n /= 2;
    		i++;
    	}
    
    	cout << "The binary equivalent is ";
    
    	//for (int j = i - 1; j >= 0; j--)
    	for (int j = 7; j >=0; j--)
    		cout << binaryNumber[j] << " ";
    
    }
    However, this program has a problem if a number greater than 255 (or a negative) number is entered. The array binaryNumber is overflowed past its boundary. Consider:

    Code:
    while (n > 0 && i < 8)
    Last edited by 2kaud; September 9th, 2019 at 03:42 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Mar 2005
    Posts
    37

    Re: My program works good but there are two things I don't understand yet

    Okay I initialized my array to all zeros first and ran the code again. But the program still does not output all eight bit places. If I enter in the integer 4 my program outputs 100. Where is the glitch?

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: My program works good but there are two things I don't understand yet

    Quote Originally Posted by 357mag View Post
    Okay I initialized my array to all zeros first and ran the code again. But the program still does not output all eight bit places. If I enter in the integer 4 my program outputs 100. Where is the glitch?
    The code in your first posts outputs leading zeroes for me.

  6. #6
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: My program works good but there are two things I don't understand yet

    Quote Originally Posted by 357mag View Post
    Okay I initialized my array to all zeros first and ran the code again. But the program still does not output all eight bit places. If I enter in the integer 4 my program outputs 100. Where is the glitch?
    Did you change the for loop as per my post #3?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Mar 2005
    Posts
    37

    Re: My program works good but there are two things I don't understand yet

    Quote Originally Posted by 2kaud View Post
    Did you change the for loop as per my post #3?
    Even if I write the while loop like you suggested my program still only outputs the bit places that represent the actual divisions that were needed. If I enter the integer 58 my program should output this:

    00111010

    But my program only outputs the actual divisions that were needed so it outputs:

    111010

    I'm still missing the first two zero's that would represent the 128 column and the 64 column.

  8. #8
    Join Date
    Mar 2005
    Posts
    37

    Re: My program works good but there are two things I don't understand yet

    I fixed it. You have to use the OR operator. Not the AND operator.

  9. #9
    Join Date
    Oct 2019
    Posts
    8

    Re: My program works good but there are two things I don't understand yet

    I tested this code with Microsoft Visual Studio. It's Ok.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured