problems converting unsigned int to binary

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//showing all bits winthin an unsigned int
#include <iostream>
using namespace std;

void showbinall(unsigned int val, int amt);

int main()
{
	unsigned int 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(unsigned int 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
start is a signed int. Signed int cannot hold as much as an unsiged (half-1). So when the last bit is flagged the number is shown as negative.
Last edited on
thanks, giving it a shot now...
Nice catch, Thanks again Zaita.
Revized code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//showing all bits winthin an unsigned int
#include <iostream>
using namespace std;

void showbinall(unsigned int val, int amt);

int main()
{
	unsigned int 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(unsigned int val, int amt)
{
	unsigned int start=1;

	for(int y=1; y < amt; y++) start *= 2;

	for(start; start > 0; start /= 2){
		if(val & start) cout << '1';
		else cout << '0';
	}
}
looks good. I'd change line 10 to
cout << "Give me a non-decimal, non-negative number please: ";

Just makes it a bit friendlier.
Topic archived. No new replies allowed.