-
September 8th, 2019, 03:50 PM
#1
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.
-
September 8th, 2019, 03:53 PM
#2
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.
-
September 9th, 2019, 03:30 AM
#3
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)
-
September 11th, 2019, 09:40 AM
#4
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?
-
September 11th, 2019, 10:22 AM
#5
Re: My program works good but there are two things I don't understand yet
Originally Posted by 357mag
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.
-
September 11th, 2019, 12:52 PM
#6
Re: My program works good but there are two things I don't understand yet
Originally Posted by 357mag
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)
-
September 12th, 2019, 12:19 PM
#7
Re: My program works good but there are two things I don't understand yet
Originally Posted by 2kaud
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.
-
September 12th, 2019, 12:21 PM
#8
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.
-
October 3rd, 2019, 03:07 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|