unsigned long long and bitset quirk

S

shaun roe

As a follow up to my question about STL and bitset<64> I'd like to share
a quirk (bug?) about unsigned long long support and the bitset. I'm
using gcc 3.2 on linux or gcc 3.3 on mac, the answer is the same.

unsigned long long int f;
f=3;
bitset<64> g(f);
cout << f << endl;
cout << g << endl;
f<<=32;
bitset<64> h(f);
g<<=32; // h and g should be the same now
cout << "f is 3 << 32: "<< f << endl;
cout << "g is (bitset<64>(3)) << 32: "<<g<< endl;
cout << "h is bitset<64>( 3 << 32): "<<h<< endl;


gives:

f is 3 << 32: 12884901888
g is (bitset<64>(3)) << 32:
0000000000000000000000000000001100000000000000000000000000000000
h is bitset<64>( 3 << 32):
0000000000000000000000000000000000000000000000000000000000000000

i.e. I cant initialise a bitset<64> from a long long: it loses the top
32 bits. But having initialised the lower bits, I can left shift them
upwards...I guess this is not very surprising, given the presently
patchy support for 64 bit ints, but still could be a nasty trap in some
situations.
 
R

Rolf Magnus

shaun said:
As a follow up to my question about STL and bitset<64> I'd like to share
a quirk (bug?) about unsigned long long support and the bitset.

Well, there is no type "unsigned long long" in C++.
I'm using gcc 3.2 on linux or gcc 3.3 on mac, the answer is the same.

unsigned long long int f;
f=3;
bitset<64> g(f);
cout << f << endl;
cout << g << endl;
f<<=32;
bitset<64> h(f);
g<<=32; // h and g should be the same now
cout << "f is 3 << 32: "<< f << endl;
cout << "g is (bitset<64>(3)) << 32: "<<g<< endl;
cout << "h is bitset<64>( 3 << 32): "<<h<< endl;


gives:

f is 3 << 32: 12884901888
g is (bitset<64>(3)) << 32:
0000000000000000000000000000001100000000000000000000000000000000
h is bitset<64>( 3 << 32):
0000000000000000000000000000000000000000000000000000000000000000

i.e. I cant initialise a bitset<64> from a long long: it loses the top
32 bits.

That's because bitset doesn't have (and isn't allowed to have) a constructor
that takes a long long.
 
A

Andrew Koenig

cout << "g is (bitset<64>(3)) << 32: "<<g<< endl;
cout << "h is bitset<64>( 3 << 32): "<<h<< endl;

3<<32 is undefined on machines with 32-bit ints.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top