473,791 Members | 2,949 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unsigned long long and bitset quirk

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:
000000000000000 000000000000000 110000000000000 000000000000000 0000
h is bitset<64>( 3 << 32):
000000000000000 000000000000000 000000000000000 000000000000000 0000

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
2 5383
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:
000000000000000 000000000000000 110000000000000 000000000000000 0000
h is bitset<64>( 3 << 32):
000000000000000 000000000000000 000000000000000 000000000000000 0000

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
"shaun roe" <sh*******@wana doo.fr> wrote in message
news:sh******** *************** ******@news-reader.wanadoop ortails.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
412
by: Juha Kettunen | last post by:
Hi I don't know if I am using right words (bit-number), but this is what I mean: You can set a 64 bit number: unsigned long a; Now you can use binary operators to manipulate variable a (for example
7
3607
by: William Payne | last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set (with set I mean they equal one). I need to determine those bits and their position and create new numbers from them. For example, consider this four-bit number: 1100 from this number I want to extract two numbers: 1000 and 100 had the four-bit number been 0101 I would want to extract 100 and 1. How should I do this? I wish I had some code to post but I don't...
5
4376
by: Rich S. | last post by:
Hi, Is the code below the best way to have the less than function for an 80 bit bitset or is there something faster / better? When I populate this map with millions (... and millions) of sets it gets slower and slower, I'm looking to make it faster. Thanks
6
8069
by: sridhar | last post by:
#include <stdio.h> int main(){ unsigned int ui = 0; if(0x0ul <= ui){ printf("less eq\n"); } } On my system unsigned long is 64 bits and unsigned int is 32.The compiler gives a warning
11
750
by: Mockey Chen | last post by:
On 32-bit platform, give you a unsigned int? what is the best way to get how many bits equal to 1. please do not use loop if possible? Thanks in advance. -- Regards. Mockey Chen Email: mockey.chen@gmail.com
4
2496
by: Sarath | last post by:
>From the documentation of MSDN, it is saying that bitset is not a STL container Unlike the similar vector<boolClass, the bitset class does not have iterators and is not an Standard Template Library container. Actaully what's so special in STL containers? What I know is that there will be certain operators overloaded, supports template meta programming, having iterators, compatible with
5
2132
by: arnuld | last post by:
i have solved the problem. any comments on it: /* C++ Primer 4/e * chapter 3 * * exercise 3.24 * STATEMENT: * consider the sequence 1,2,3,5,13,21. Initialize a "bitset<32>" object that has a one bit in each position corresponding to a number in this sequence. */
1
8007
by: krishna81m | last post by:
I am a newbie and have been trying to understand conversion from double to int and then back to int using the following code was posted on the c++ google group. Could someone help me out with understanding why and how a double can be represented in bits and bytes and which of the following can allow us to view the binary representation, unsigned char representation and then convert back to int please... #include<iostream> #include<iomanip>...
5
2346
by: swcamry | last post by:
class bitset::reference { friend class bitset; reference(); // no public constructor public: ~reference(); operator bool () const; // convert to bool reference& operator= ( bool x ); // assign from bool reference& operator= ( const reference& x ); // assign from bit reference& flip(); // flip bit value
0
10427
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9995
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.