Array problem in C++
using namespace std;
void binary(int number);
int main() {
int b;
for (int x = 0; x < 256; ++x) {
binary(x);
if (x < 128)
cout << " " << x << " " << x <<endl;
else cout << " " << x << " " << 127 - x <<endl;
}
cin >> b;
return 0;
}
void binary(int number) {
int bitTable [8], y;
for (y = 0; number != 0; y++) {
bitTable[y] = number%2;
number /= 2;
}
while(y < 8) {
bitTable[y] = 0;
y++;
}
for (int j = y - 1; j >= 0; j--) {
int bit = bitTable[j];
cout << bit;
}
}
The above code is using an array to store bits as they are converted from decimal numbers.. But something is wrong.
The first number displayed is always some huge number. So for 255 decimal I should be getting 11111111, instead I get something like 378796481111111. I have tried setting the last element to 0(since they're being printed out in reverse), or not display it at all.. But then the one before this becomes a huge number instead, and it just removes one of the bits instead. Nothing seems to be working to fix it. Please help! (I am using Bloodshed Dev-cpp 4.9.9.2, assuming that information is important)
EDIT: Added the code tags I forgot.

