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

• September 8th, 2019, 03:50 PM
357mag
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] << " "; }```
• September 8th, 2019, 03:53 PM
357mag
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.
• September 9th, 2019, 03:30 AM
2kaud
Re: My program works good but there are two things I don't understand yet
Quote:

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)`
• September 11th, 2019, 09:40 AM
357mag
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
GCDEF
Re: My program works good but there are two things I don't understand yet
Quote:

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?

• September 11th, 2019, 12:52 PM
2kaud
Re: My program works good but there are two things I don't understand yet
Quote:

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?
• September 12th, 2019, 12:19 PM
357mag
Re: My program works good but there are two things I don't understand yet
Quote:

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
357mag
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
BenCussen
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.