Biggest C++ Data Type?
Whats the biggest standered C++ Data Type. I need it unsigned,
so 0-AS_BIG_AS_IT_GETS,
Im guessing it a double, but Im not sure, how big IS a double?
Thanks a lot,
A double should be 2 BYTES (normally). If I''m not mistaken...
A DWORD is 4 BYTES though...
A DWORD is 4 BYTES though...
quote: Original post by Esap1
Whats the biggest standered C++ Data Type. I need it unsigned,
so 0-AS_BIG_AS_IT_GETS,
Im guessing it a double, but Im not sure, how big IS a double?
Thanks a lot,
A double is NOT an integer; it is floating-point, at least 4 bytes (and possibly 8), and definitely signed. Most likely not what you want.
The largest standard C++ integer type is long , usually 4 bytes on a 32-bit system.
---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
Assuming you are on a 32-bit machine;
A double IS NOT 2 bytes. According to the IEEE 754 floating point standard that C++ uses a double is 64-bits. Ie. it is 8 bytes. A float is 4 bytes.
If you need a unsigned integer you should use:
unsigned long long
it is not available in all compilers, but that is 64-bit too. Beware though, operations using long long''s are slow in 32-bit code.
Jacob Marner
A double IS NOT 2 bytes. According to the IEEE 754 floating point standard that C++ uses a double is 64-bits. Ie. it is 8 bytes. A float is 4 bytes.
If you need a unsigned integer you should use:
unsigned long long
it is not available in all compilers, but that is 64-bit too. Beware though, operations using long long''s are slow in 32-bit code.
Jacob Marner
Jacob Marner, M.Sc.Console Programmer, Deadline Games
in case your wondering, an unsigned long can range from 0 -4,294,967,296
There''s always the __int64 type, but that''s Microsoft-specific, not ANSI standard.
-Ironblayde
Aeon Software
-Ironblayde
Aeon Software
"Your superior intellect is no match for our puny weapons!"
The new C99 standard provides a new type: long long, which when unsigned ranges from 0 to 18446744073709551615. But that''s C, not C++, and there are currently no c99-compliant c compilers. You''ll have to stick with long for now, if you''re loking for a portable solution.
If a long doesn''t suffice, you _could_ write one of those "big number" classess... But that''ll probably be pretty slow.
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
#include
using std::cout;
int main(int, char*)
{
// integer types
cout << sizeof(char) << endl;
cout << sizeof(short) << endl;
cout << sizeof(int) << endl;
cout << sizeof(long) << endl;
// floating-point types
cout << sizeof(float) << endl;
cout << sizeof(double) << endl;
cout << sizeof(long double) << endl;
return 0;
}
That'll give you the size, in bytes, of each type. To find out the largest number an integer type can store, there are two formulas (assuming its size is > 1 byte):
1. For unsigned numbers (simplest)
Take the size in bytes to the 16th power, like so:
// sizeof(unsigned short) == 2; on Win32
sizeof(unsigned short)^16 = 65536; // Win32 size
// stores values from 0 to +65536
2. For signed numbers
Take the size in bytes to the 16th power, like before, then divide it in half and reverse its sign to find the minimum value, then to find the maximum value you take the size in bytes to the 16th power, divide it in half, and subtract one to find the maximum value:
// sizeof(short) == 2; on Win32
-((sizeof(short)^16) / 2) = -32768;
((sizeof(short)^16) / 2) - 1 = +32767;
// stores values from -32768 to +32767
You subtract one because of 0; that is, -32768 to -1 is 32768 places, and 0 to 32767 is also 32768 places, which equals 65536. The reason why that formula works is that signed numbers use the highest-value bit to indicate the sign, which chops the number almost exactly in half.
Oh, and don't use the exponent sign (^) in C\C++, because it's a bitwise operator, which is something very different.
- null_pointer
Sabre Multimedia
Edited by - null_pointer on September 9, 2000 8:25:58 AM
OK, I feel really stupid but, what''s the difference between signed and unsigned? I never learned it, though I probably should, eh?
+H3 FU+UR3 ØF MUZ!K MUZ+ NØ+ B37ØNG +Ø +H3 m3D!ØCr3
+H3 FU+UR3 ØF MUZ!K MUZ+ NØ+ B37ØNG +Ø +H3 m3D!ØCr3
"Now watch as I run away in a womanly fashion." - Batman
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement