Hello there everybody..
I am just trying to understand something in relation to bitshift operations. With reference to the small program below. I have tried to keep this as simple as possible.
I understand that by doing this (integer << x), a temporary variable is formed to contain the result of the new value of the integar where all bits have been moved to the left (from least significant to the most significant bit) by x places.
I'm just not sure I understand why when after the 32 bits have been exceeded (I'm using a 32 bit system, int = 32 bits), that the operation seems to start from scratch again. I thought all the bits would just be pushed off the edge of the 32 bits and the remaining output would be zero.
If you see the output below the procedure below, after 32 cycles, it starts again. Is it true that the bitshift operations will continually loop through the 32 bits and return to the start after the 32 bits have been exceeded? Is there another explanation?
int t = 1;
for (int p = 0; p < 40; p++)
{
cout << "Count: " << p << " - "<< (t << p) << endl;
}
Output:
Count: 0 - 1
Count: 1 - 2
Count: 2 - 4
Count: 3 - 8
Count: 4 - 16
Count: 5 - 32
Count: 6 - 64
Count: 7 - 128
Count: 8 - 256
Count: 9 - 512
Count: 10 - 1024
Count: 11 - 2048
Count: 12 - 4096
Count: 13 - 8192
Count: 14 - 16384
Count: 15 - 32768
Count: 16 - 65536
Count: 17 - 131072
Count: 18 - 262144
Count: 19 - 524288
Count: 20 - 1048576
Count: 21 - 2097152
Count: 22 - 4194304
Count: 23 - 8388608
Count: 24 - 16777216
Count: 25 - 33554432
Count: 26 - 67108864
Count: 27 - 134217728
Count: 28 - 268435456
Count: 29 - 536870912
Count: 30 - 1073741824
Count: 31 - -2147483648
Count: 32 - 1
Count: 33 - 2
Count: 34 - 4
Count: 35 - 8
Count: 36 - 16
Count: 37 - 32
Count: 38 - 64
Count: 39 - 128