+ 2
Why answer of this code is 8?
#include <iostream> using namespace std; int main() { int a=0; a=1<<3; cout<<a; return 0; }
2 Answers
+ 5
because 1 = 0001.
And your bitshift pushed the 1, 3 times to the left.
8 = 1000.
+ 1
This is called bitwise operator, 1<<3 mean move 3 spaces to left the bit.
1 - Original
10 - One moved
100 - Two moved
1000 - Three moved.
Now by default when printed on scren is showed on decimal, this mean you need transform from binary to decimal.
2^3 = 8
Extra: Bitwise accept number input on decimal also: Check if you want to eval:
a = 4<<3
You need first param in binary this mean
a = 100<<3
Now move three spaces to left
a = 100 000
a = 2^5 = 32
Hope it helps.