Singed/Unsigned Variable types (C Question)

Started by
13 comments, last by steveharper101 22 years, 7 months ago
Thanks for the post's guy's!!

So an unsigned integer can store values from 2^32 = 4294967296 possible values from 0 - 4294967296.

A signed integer can store values from 2^31 = 2147483648
possible values from -2147483648 to 2147483647
where one of the bits equals the singed value

These values can vary from compiler to compiler

And short int's are half these values as there are only 16 bits long

Is this correct?

Thanks Alot for the previous posts!!

~StevE~


Edited by - steveharper101 on September 6, 2001 6:41:36 PM
Advertisement
>>So an unsigned integer can store values from 2^32 = 4294967296 possible values from 0 - 4294967296.<<

u cant always be certain, eg 64bit computers are becoming more common and an int there is usually 2^64
quote:Original post by steveharper101
And short int''s are half these values as there are only 16 bits long

Is this correct?


16-bit ints have half the number of bit, and so the min/max values is the square root of the min/max value of a 32-bit int.

int: 2^32 = ~4,000,000,000
short: 2^16 = ~65,000

codeka.com - Just click it.
Yeah thanks your right about the 16bit integer math

Thanks

~Steve~
quote:Original post by Absolution
Umm, they are actually synonyms for ANSI standards so they ensure you are using the right type. Or to quote the SDK "useful for writing portable code that behaves identically across multiple platforms". Anyway, the only reason I bring it up is because I have had occasion recently to use the __int64 type while writing the timing code for my game (the high performance counters all return 64 bit integers). I happened to notice in the SDK that you can use this syntax for all types. I have only used __int64 myself, but I can see how the others might be helpful so I thought I would point them out. Look it up yourself if you don't believe me.

Oh yeah, one last thing, I don't think littering your code with #ifdef's is the best way to go for cross-platform compilations, but that's for another thread.

Abs

Yes, synonyms for ANSI types, not ANSI types per se. My point is that those synonyms are defined by Microsoft and not in the standard (at least this is how it used to be, if they are currently supported by other compilers, or perhaps even the standard, then please correct me).
I believe that what the SDK docs mean is that using the __int8/16/32/64 types will ensure portability between different windows platforms (x86/alpha, win32/win64), not portability in the broader sense.

Currently the "most portable" way to write portable code is to use #idefs and #defines for some things, the reason being that there are many (small) incompatibilities in the implementation of different compilers/platforms/run-time libraries.

A small example:
In MSVC the 64 bit integer type is called "__int64", in GCC it's called "long long" (for a 32bit platform).



Edited by - Dactylos on September 17, 2001 4:34:52 PM

This topic is closed to new replies.

Advertisement