General discussion

Locked

Problems with C++ 32-bit integers

By pokimaniac250 ·
Hi,
I was trying to make a function called show_binary() that would display the binary representation of the unsigned integer sent to it in its parameters. Here is my code:

#include <iostream>
#include <cmath>
using namespace std;

void show_binary (unsigned int num);

int main() {
unsigned int i = 0;

cout << "Enter an integer: ";
cin >> i;
cout << '\n';

show_binary(i);

return 0;
}

void show_binary (unsigned int num) {
unsigned int x;


for (x = (pow(2.0, (int) (sizeof (unsigned int))*8)) ;x > 0;x /= 2){
if (num&x) cout << "1 ";
else cout << "0 ";
}

}

When I ran this in the Visual Studio Command Prompt, the FOR loop did not run and nothing was displayed by the function. However when I lowered the initial value of x to only show 24 or less bits of the integer, it worked fine. I even tried putting 4,294,967,296 (2 raised to 32) and had the same result. Visual Studio uses 32 bit integers and "pow(2.0, (int) (sizeof (unsigned int))*8)" does in fact result in 4,294,967,296 so this should work.

Any suggestions?

-James Waltz

This conversation is currently closed to new comments.

5 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

by pokimaniac250 In reply to Problems with C++ 32-bit ...

It seems no one knows my answer so I'm putting in more points.

Collapse -

by j.lupo In reply to Problems with C++ 32-bit ...

Ok, this is going to sound strange, but what is the capability of your PC. As I recall, I had something similar happen and it turned out to be a machine specific issue. When I ran the code on my Unix box, it worked fine, but when I ran it on my PC, it didn't.

So, that is just a thought. You are correct from a mathematical point, but check in case your PC can't handle that percision.

Good Luck.

Collapse -

by pokimaniac250 In reply to Problems with C++ 32-bit ...

Hm, I'm not quite sure what you mean by my system specifications but it would make sense that the machine may simply be incapable of handling this program. Though you wouldn't think 32 bits are such a big deal -_-

Collapse -

by ?/\/\?|???\/??? In reply to Problems with C++ 32-bit ...

Hints:
What is the type of x, and what type does pow return?

What value does (pow(2.0, (int) (sizeof (unsigned int))*8)) return, and what is the value of x after initializing it?

Also... Try:
cout << UINT_MAX;

Collapse -

by djidji200 In reply to Problems with C++ 32-bit ...

There is always a problem if you mix signed and unsigned ints in a statement, and definitely if you use floats or doubles too. Always be carefull for implicit conversions. Unsigned types have precedence over signed types.
Be aware that a float only has 24 bits of mantisse. An unsigned int can contain a larger whole number than a float. For a double the mantisse is 53 bits. (See float.h)
The function "double pow(double,int)" is the problem.
The value of pow(2.0,32) does NOT fit in an unsigned int. The max number that an unsigned int can store is 2^32-1.
If you only calculate with ints and bits stick with ints.
What you want to do is to set bit 31 of x.

A better 'for' loop is

for (x= (1<<(sizeof(unsigned int)*8-1)); x; x>>=1) {
cout << ( (num&x) ? "1" : "0" );
}

In Visual C 'int' and 'long' are both 32 bits.
If you want larger ints in Visual Studio use
long long
__int64

They also come in unsigned versions.

Look at __int64 and then 'Fundamental Types' in the Help.

Mario Veraart

Back to Networks Forum
5 total posts (Page 1 of 1)  

Related Discussions

Related Forums