By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,315 Members | 1,672 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,315 IT Pros & Developers. It's quick & easy.

unsigned long long and bitset quirk

P: n/a
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:
00000000000000000000000000000011000000000000000000 00000000000000
h is bitset<64>( 3 << 32):
00000000000000000000000000000000000000000000000000 00000000000000

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.
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
shaun roe wrote:
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:
00000000000000000000000000000011000000000000000000 00000000000000
h is bitset<64>( 3 << 32):
00000000000000000000000000000000000000000000000000 00000000000000

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.
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.


Jul 22 '05 #2

P: n/a
"shaun roe" <sh*******@wanadoo.fr> wrote in message
news:sh*****************************@news-reader.wanadooportails.com...
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.
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.