ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Hello,
I have to use an array of unsigned integers as a bitset.
This bitset is involved in a long series of some logic operations
like 'or' and 'and'.
The dilemma is what type to use.
If I choose simply 'unsigned int', then the compiler could give me
a 32-bit type even on a 64-bit machine.
If I choose something like 'uint64' or 'unsigned long long',
the compiler give me a 64-bit type, but on 32-bit machine perhaps
it is more convenient to work with 32-bit type, in this case.
So what type should I use as the largest unsigned type the machine
could naturally handle?
I looked if there is some type defined for such tasks like some c99
specific new types are, but I didn't find an answer.
Have you any ideas?
Thanks,
tano
Click here to see the post LQ members have rated as the most helpful post in this thread.
I don't think there is an easy way out here. What you would want to do is put in a bit of code that first determines whether the machine is a 64 bit or 32 bit OS, and then depending on the answer, attempts to create a type matching the bit depth of the machine (attempting to create multiple types until one that matches is found if necessary, but eventually giving up and defaulting to the best available type if the desired length can't be found).
So what type should I use as the largest unsigned type the machine
could naturally handle?
I don't know of any general answer to that. I don't think there is a general answer.
unsigned long gives you the best size on 32 bit x86 and on x86_64 and on most common modern systems. But there are systems where unsigned int would be a different and better choice than unsigned long.
unsigned int is usually the most "natural" size unsigned type for an architecture. Typically that means selecting a larger or smaller size will tend to allow fewer operations per second. That is actually true on x86_64. unsigned long (64 bits) does tend to be slower than unsigned int (32 bits). But that difference is very subtle.
If I understand your purposes, you would prefer unsigned int over unsigned long only if unsigned long were nearly twice as slow per operation (meaning similar speed per bit). If unsigned long is barely slower per operation (nearly twice as fast per bit) which it is in x86_64, then I assume you prefer unsigned long.
On 32 bit x86, both unsigned int and unsigned long are 32 bit, so the generated code and the resulting speed are identical.
Why not just use unsigned char[]? Hopefully you know exactly what bit operations you need, so you can cast to the appropriate width before performing operations. Otherwise, I'm sure you know your minimum required width, so go with that if nothing else.
Kevin Barry
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.