Hello,
I'm trying to show an unsigned int's binary equivelant. The problem is that I need to show all then numbers, not just the first eight bits. I made a program to find out how many bytes in an unsigned int in it's current environment. Next I put it into a function that tries to show it in binary. I added a couple cout statements to see what's happening... Heres the code:
//showing all bits winthin an unsigned int
#include <iostream>
usingnamespace std;
void showbinall(unsignedint val, int amt);
int main()
{
unsignedint inp; int num=0;
cout << "Give me a non-decimal, non-negative number please";
cin >> inp;
cout << "In your environment, an unsigned int is " << sizeof inp << " bytes.\n";
num = sizeof inp;
num *= 8;
cout << "These are all the bits in " << inp << ": ";
showbinall(inp, num);
return 0;
}
void showbinall(unsignedint val, int amt)
{
int start=1;
for(int y=1; y < amt; y++){ start *= 2; cout << "\nstart is: " << start;}
cout << "\nstart is: " << start << '\n';
for(; start < 0; start /= 2){
if(val & start) cout << '1';
else cout << '0';
}
}
**TRIAL RUN**
Input:
22
Output:
Give me a non-decimal, non-negative number pleaseIn your environment, an unsigned int is 4 bytes.
These are all the bits in 22:
start is: 2
start is: 4
start is: 8
start is: 16
start is: 32
start is: 64
start is: 128
start is: 256
start is: 512
start is: 1024
start is: 2048
start is: 4096
start is: 8192
start is: 16384
start is: 32768
start is: 65536
start is: 131072
start is: 262144
start is: 524288
start is: 1048576
start is: 2097152
start is: 4194304
start is: 8388608
start is: 16777216
start is: 33554432
start is: 67108864
start is: 134217728
start is: 268435456
start is: 536870912
start is: 1073741824
start is: -2147483648
start is: -2147483648
00000000000000000000000000011111
Why am I getting negatives for start? Thanks for taking a look,
enduser000