In a code I have been editing the previous programmer used a shift operator to add a moderately large number to a size_t integer. When I played with this particular integer for debugging purposes I found that changing the number did not yield predicable results.
Input:
std::size_t
foo1 = 100000 << 20,
foo2 = 200000 << 20,
foo3 = 300000 << 20,
foo4 = 400000 << 20;
std::cout << "foos1-4:";
std::cout << foo1;
std::cout << foo2;
std::cout << foo3;
std::cout << foo4;
yields:
foos1-4:
1778384896
18446744072971354112
1040187392
18446744072233156608
I'm know it is some kind of overflow error, but (to my admittedly limited knowledge) size_t is not supposed to have those. From what I understand size_t is an unsigned integer type that is able to hold a virtually unlimited number of integers.
From what I understand of bit-shift operators this code should be multiplying the number by 2^20 (1048576). Links to other pages on this site: What are bitwise shift (bit-shift) operators and how do they work?
Note - I worked out by hand that foo1 appears to be an overflow error with a 32 binary digit truncation, but all the others seem totally random to me.
From http://en.cppreference.com/w/cpp/types/size_t: std::size_t can store the maximum size of a theoretically possible object of any type (including array). From that I assume the issue must be in either how the integers are declared or how bit shift is operating.
What's going on?