Advertisement

Biggest C++ Data Type?

Started by September 08, 2000 06:50 PM
16 comments, last by Esap1 24 years, 3 months ago
Signed values can be both negative and positive (one of the bits are used to indicated if the number is negative or positive, this bit is called the sign bit). Unsigned numbers don''t have a sign bit, and therefore they can only be positive.
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
Hm, well then how come I should use signed vs. unsigned, or the otherway around? It's just one bit....stupid computers (ahg! blasphemy!)

So is there something else to it besides the -/+ dilly, or is that all? There's gotta be a reason I accessed A000:0000 in DOS with an unsigned char instead of a signed...I hope.

(+10 for the person who can guess what the good 'ol A000:0000 is. )

+H3 FU+UR3 ØF MUZ!K MUZ+ NØ+ B37ØNG +Ø +H3 m3D!ØCr3

Edited by - Sponge99 on September 9, 2000 9:51:56 AM
"Now watch as I run away in a womanly fashion." - Batman
Advertisement
Hmmm quite right, but only quite ... it''s not one bit indicatinc the sign.
If it were so then e.g. (8 bit signed ) 00000001 would be 1 and 10000001 would be -1, this is not true.

positive numers are represneted as normal e.g. 00010110, but negative numbers are represented as 11111111 (+1) - ( the postive number number) so 11111111 is -1. This way the CPU can apply the same operation for adding/subtracting sigend values as for unsigned values. The most simple example is the inc operation.

incing 11111111 overflows to 00000000 which is correct -1 + 1 = 0.

C ya
Lightsaber
I just thought I should add to LightSaber''s post: That is what prevents the equals (==) operator from thinking there is a difference between 0 and -0, which, of course, doesn''t exist. I think Lightsaber did it right... I haven''t checked, but if all is correct, there is only 0 and not -0.

--------------------


You are not a real programmer until you end all your sentences with semicolons;

Yanroy@usa.com

Visit the ROAD Programming Website for more programming help.

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

quote:
To find out the largest number an integer type can store...


Actually there''s a much easier way in C++, you just need to use the numeric_limits traits class. Something like this should give you what your looking for:

    #include <limits>#include <iostream>using std::cout;using std::endl;int main(){	cout << "max int = " << std::numeric_limits<int>::max() << endl;	cout << "max unsigned int    = " << std::numeric_limits<unsigned int>::max() << endl;	cout << "max long = " << std::numeric_limits<long>::max() << endl;	cout << "max double = " << std::numeric_limits<double>::max() << endl;	return 0;}    


numeric_limts should be specialized for each of the types on your compiler. If your not using C++, you can get the same results via limits.h which contains a load of macro definitions for each of the types.
One hint to keep in mind. Look at this loop:

    //...somewhere...signed int index = 5;  //...somewhere else...while (index >= 0) {  //...do stuff...  --index;}    


This works just fine. But let''s say you were looking only at the declaration and said to yourself "index should never be negative, so I can get a larger range by making it unsigned." So you happily change index from a signed int to unsigned and...

...and your program never leaves the loop. Suddenly, the loop termination condition breaks down because when index == 0 and you do --index, you underflow to the largest possible unsigned integer which is still positive.

This is a very simplistic example, but I''ve been hit by it myself and have seen others do it in various forms. There are no (easy) ways to check for under/overflow with unsigned integers. And since most people don''t need that one extra bit of number storage (31 bits gets you over 2 million), you may be better off always using signed.

Then again, know your purpose. For longs and shorts I almost always use signed, but if I want to represent a generic byte-stream, I use unsigned char so my range is 0 to 255 rather than -128 to 127.

---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
Advertisement
Aaah, thanks. Starting to make sense.

+H3 FU+UR3 ØF MUZ!K MUZ+ NØ+ B37ØNG +Ø +H3 m3D!ØCr3
"Now watch as I run away in a womanly fashion." - Batman
Wilka''s post has the right way to find out limits in C++ ... OR simple use the C subset system ... there are a simple set of constants in (which is what the compiler includes with ) and this way ... you know .. even if you''ve in C ... you can simply query standard defines like:

INT_MAX
INT_MIN
CHAR_MAX
..
SHRT_MAX
..
LONG_MAX
ULONG_MAX
etc. etc.

see the basic pattern yet

note also that Visual C++ provides the /J compiler option to make the char variable unsigned by defualt ... but that could trip up other code if you''re not carefull, so i just have a little header that defines an unsigned version of each integral type as:
uchar
uint
... you get the idea ...
and i use those where I want unsigned numbers.

BTW .. for those who don''t understand reasons to care about signed unsigned ... first of all ... if you are going UP from zero .. to some deffinate maximum .. then UNSIGNED numbers prevent the possibility that a wraparound will produce a completely unexpected negative number (which in pointer aritmatic like array subscripts is a NO-NO). if you are counting up .. but do NOT have a deffinate limit ... and also are not going to be worse off with a negative number than an invalid high number ... then SIGNED numbers help you detect accidental wraparound. there are other cases ... but that''s enough to get you thinking.

This topic is closed to new replies.

Advertisement