LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-24-2009, 12:58 PM   #1
tanoatlq
Member
 
Registered: Mar 2007
Posts: 157

Rep: Reputation: 30
c unsigned type


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.
Old 11-24-2009, 02:58 PM   #2
davidstvz
Member
 
Registered: Jun 2008
Posts: 405

Rep: Reputation: 31
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).
 
Old 11-24-2009, 03:16 PM   #3
tanoatlq
Member
 
Registered: Mar 2007
Posts: 157

Original Poster
Rep: Reputation: 30
Thanks! I'll check for macros, then.
 
Old 11-24-2009, 03:32 PM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by tanoatlq View Post
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.

Last edited by johnsfine; 11-24-2009 at 03:34 PM.
 
Old 11-24-2009, 07:19 PM   #5
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by tanoatlq View Post
what type should I use as the largest unsigned type the machine
could naturally handle?
Go with unsigned long long. Then during program initialization, find sizeof(unsigned long long) to see how large it is.
 
1 members found this post helpful.
Old 11-24-2009, 08:18 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 11-25-2009, 03:18 PM   #7
tanoatlq
Member
 
Registered: Mar 2007
Posts: 157

Original Poster
Rep: Reputation: 30
It seems the matter is more complex than I think.
Thanks.
 
1 members found this post helpful.
Old 11-27-2009, 04:34 PM   #8
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Quote:
Originally Posted by tanoatlq View Post
So what type should I use as the largest unsigned type the machine could naturally handle?
size_t will give you an integer the same size as a pointer.

You may also want to consider the C99 typedefs uint_fast16_t or uint_fast32_t, which guarantee a minimum size but may be larger if it's "faster".
 
3 members found this post helpful.
Old 11-28-2009, 12:20 PM   #9
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
So what type should I use as the largest unsigned type the machine could naturally handle?
32 Bit.
(Unless you are using an 8 bit processor LOL)
 
0 members found this post helpful.
Old 11-28-2009, 02:47 PM   #10
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Quote:
Originally Posted by smeezekitty View Post
32 Bit.
(Unless you are using an 8 bit processor LOL)
How do you figure that ?

For me both sizeof(unsigned long long) and sizeof(unsigned long) are 8 bytes long (x86_64). I'd think that's the maximum size.
 
0 members found this post helpful.
Old 11-28-2009, 02:56 PM   #11
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by H_TeXMeX_H View Post
How do you figure that ?

For me both sizeof(unsigned long long) and sizeof(unsigned long) are 8 bytes long (x86_64). I'd think that's the maximum size.
64bit only comes in signed mode on a 16 bit processor
 
0 members found this post helpful.
Old 11-28-2009, 03:01 PM   #12
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Quote:
Originally Posted by smeezekitty View Post
64bit only comes in signed mode on a 16 bit processor
to this I should probably say: wtf ?

but instead I will say: mmmhhhhmmmmm ... I see ...
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
unsigned long int uint32_t to unsigned char and back MicahCarrick Programming 2 08-02-2009 01:39 AM
signed and unsigned in C noir911 Programming 10 06-02-2009 08:58 AM
unsigned integers in C CoderMan Programming 5 03-24-2009 07:50 PM
signed and unsigned ArthurHuang Programming 4 05-23-2006 03:46 AM
convert unsigned char * to unsigned long int linux_lover2005 Programming 3 04-26-2005 11:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration