Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird result after assigning 2^31 to a signed and unsigned 32-bit integer variable

As the question title reads, assigning 2^31 to a signed and unsigned 32-bit integer variable gives an unexpected result.

Here is the short program (in C++), which I made to see what's going on:

#include <cstdio>
using namespace std;

int main()
{
    unsigned long long n = 1<<31;
    long long n2 = 1<<31;  // this works as expected
    printf("%llu\n",n);
    printf("%lld\n",n2);
    printf("size of ULL: %d, size of LL: %d\n", sizeof(unsigned long long), sizeof(long long) );
    return 0;
}

Here's the output:

MyPC / # c++ test.cpp -o test
MyPC / # ./test
18446744071562067968      <- Should be 2^31 right?
-2147483648               <- This is correct ( -2^31 because of the sign bit)
size of ULL: 8, size of LL: 8

I then added another function p(), to it:

void p()
{
  unsigned long long n = 1<<32;  // since n is 8 bytes, this should be legal for any integer from 32 to 63
  printf("%llu\n",n);
}

On compiling and running, this is what confused me even more:

MyPC / # c++ test.cpp -o test
test.cpp: In function ‘void p()’:
test.cpp:6:28: warning: left shift count >= width of type [enabled by default]
MyPC / # ./test 
0
MyPC /

Why should the compiler complain about left shift count being too large? sizeof(unsigned long long) returns 8, so doesn't that mean 2^63-1 is the max value for that data type?

It struck me that maybe n*2 and n<<1, don't always behave in the same manner, so I tried this:

void s()
{
   unsigned long long n = 1;
   for(int a=0;a<63;a++) n = n*2;
   printf("%llu\n",n);
}

This gives the correct value of 2^63 as the output which is 9223372036854775808 (I verified it using python). But what is wrong with doing a left shit?

A left arithmetic shift by n is equivalent to multiplying by 2n (provided the value does not overflow)

-- Wikipedia

The value is not overflowing, only a minus sign will appear since the value is 2^63 (all bits are set).

I'm still unable to figure out what's going on with left shift, can anyone please explain this?

PS: This program was run on a 32-bit system running linux mint (if that helps)

like image 783
Rushil Paul Avatar asked Apr 02 '12 08:04

Rushil Paul


People also ask

How many decimal digits could be stored in a signed 32 bit integer?

A 32-bit integer limit allows for 4,294,967,296 (232 2 3 2 ) pieces of data. If storing signed integers, this would range from -2,147,483,648 to 2,147,483,647.

Which data type can hold a 32 bit unsigned integer?

Integer, 32 Bit BCD: Unsigned Binary Coded Decimal value ranging from 0 to +99999999. Integer, 32 bit BCD data type is used for numerical tags where variables can only represent in the range from 0-9 within the half-byte boundary.

How do you declare a 32 bit signed integer in C ++?

Microsoft C/C++ features support for sized integer types. You can declare 8-, 16-, 32-, or 64-bit integer variables by using the __intN type specifier, where N is 8, 16, 32, or 64.

What are the ranges of 8-bit 16 bit 32 bit and 64 bit integer in unsigned and signed representation?

An 8-bit unsigned integer has a range of 0 to 255, while an 8-bit signed integer has a range of -128 to 127 - both representing 256 distinct numbers. It is important to note that a computer memory location merely stores a binary pattern.


2 Answers

On this line:

unsigned long long n = 1<<32;

The problem is that the literal 1 is of type int - which is probably only 32 bits. Therefore the shift will push it out of bounds.

Just because you're storing into a larger datatype doesn't mean that everything in the expression is done at that larger size.

So to correct it, you need to either cast it up or make it an unsigned long long literal:

unsigned long long n = (unsigned long long)1 << 32;
unsigned long long n = 1ULL << 32;
like image 71
Mysticial Avatar answered Sep 30 '22 03:09

Mysticial


The reason 1 << 32 fails is because 1 doesn't have the right type (it is int). The compiler doesn't do any converting magic before the assignment itself actually happens, so 1 << 32 gets evaluated using int arithmic, giving a warning about an overflow.

Try using 1LL or 1ULL instead which respectively have the long long and unsigned long long type.

like image 28
orlp Avatar answered Sep 30 '22 03:09

orlp