'C++ Left Shift Operation Bit Manipulation
I am not able to get why it is giving negative after certain point. Also I am using long long to prevent overflow.
Code
#include <iostream>
using namespace std;
int main() {
for(long long i=0;i<64;i++){
cout << 1LL*(1<<i) << " ";
}
return 0;
}
Output
1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912 1073741824 -2147483648 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912 1073741824 -2147483648
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
