LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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
 
LinkBack Search this Thread
Old 11-24-2009, 01: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, 03:58 PM   #2
davidstvz
Member
 
Registered: Jun 2008
Posts: 399

Rep: Reputation: 30
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, 04: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, 04:32 PM   #4
johnsfine
Senior Member
 
Registered: Dec 2007
Distribution: Mepis, Centos
Posts: 3,837

Rep: Reputation: 685Reputation: 685Reputation: 685Reputation: 685Reputation: 685Reputation: 685
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 04:34 PM.
 
Old 11-24-2009, 08:19 PM   #5
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: Debian lenny, Slackware 12
Posts: 805

Rep: Reputation: 161Reputation: 161
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, 09:18 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: Slackware64 13.37, Kubuntu 10.04
Posts: 2,831

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, 04: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, 05:34 PM   #8
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 36
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, 01:20 PM   #9
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: washington U.S.
Distribution: Damn Small Linux, KateOs, M$ Ickdows Vista, My own OS
Posts: 2,094

Rep: Reputation: 104Reputation: 104
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, 03:47 PM   #10
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 10,999
Blog Entries: 2

Rep: Reputation: 748Reputation: 748Reputation: 748Reputation: 748Reputation: 748Reputation: 748Reputation: 748
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, 03:56 PM   #11
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: washington U.S.
Distribution: Damn Small Linux, KateOs, M$ Ickdows Vista, My own OS
Posts: 2,094

Rep: Reputation: 104Reputation: 104
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, 04:01 PM   #12
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 10,999
Blog Entries: 2

Rep: Reputation: 748Reputation: 748Reputation: 748Reputation: 748Reputation: 748Reputation: 748Reputation: 748
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
Trackbacks are Off
Pingbacks are On
Refbacks are 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 02:39 AM
signed and unsigned in C noir911 Programming 10 06-02-2009 09:58 AM
unsigned integers in C CoderMan Programming 5 03-24-2009 08:50 PM
signed and unsigned ArthurHuang Programming 4 05-23-2006 04:46 AM
convert unsigned char * to unsigned long int linux_lover2005 Programming 3 04-27-2005 12:38 AM


All times are GMT -5. The time now is 10:12 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration