I’m making a floating_point class and am trying to access the last bit in an unsignedlong by masking it by (1 << 64), but when I compile and run it stops me saying that my value is too big, even though it’s exactly within the constraints! I checked the size of my unsigned long and it said 8 too so I don’t get it, mind helping me?
1. unsigned long is a 32-bit integer even on some 64-bit platforms.
2. 2^64 cannot be held by a 64-bit integer. Did you mean to 2^63?
3. 1 is still of type int. You need to convert it to a suitably large type first in order to nor overflow with the shift.
If you insist on using unsignedlong as your type you should know that there is a literal suffix ul that you can use to turn integer literals into that type.
Note that numeric_limits<>::digits is the number of non-sign bits in an integer type:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <limits>
usingnamespace std;
int main()
{
cout << "char has " << numeric_limits<char>::digits << " non-sign bits\n";
cout << "unsigned char has " << numeric_limits<unsignedchar>::digits << " non-sign bits\n";
cout << "short has " << numeric_limits<short>::digits << " non-sign bits\n";
cout << "unsigned short has " << numeric_limits<unsignedshort>::digits << " non-sign bits\n";
cout << "int has " << numeric_limits<int>::digits << " non-sign bits\n";
cout << "unsigned int has " << numeric_limits<unsignedint>::digits << " non-sign bits\n";
cout << "long has " << numeric_limits<long>::digits << " non-sign bits\n";
cout << "unsigned long has " << numeric_limits<unsignedlong>::digits << " non-sign bits\n";
}
char has 7 non-sign bits
unsigned char has 8 non-sign bits
short has 15 non-sign bits
unsigned short has 16 non-sign bits
int has 31 non-sign bits
unsigned int has 32 non-sign bits
long has 63 non-sign bits
unsigned long has 64 non-sign bits