+ 3
Please explain this question???
#include <iostream> using namespace std; int main() { int x=17; cout << (x>>2); } why the output is 4???
5 Answers
+ 4
Doing x>>2 is the same as doing x/(2^2) so 17/4 is 4
It is a binary shift to the right :
17 = 1*16 + 0*8 + 0*4 + 0*2 + 1*1
= 1*2^4 + 0*2^3 + 0*2^2 + 0*2^1 + 1*2^0
=(10001)2
Now, we shift the bits to the right and we obtain
17>>2 = 100|01
the part on the right of | is just ignored and we keep only 100
(100)2 = 2^2 + 0*2^1 + 0*2^0
= 4
+ 3
A resource for it
http://www.cprogramming.com/tutorial/bitwise_operators.html
+ 1
the output is 68
"x << 2" is a bit wise operation
=
2^2 = 4
+ 1
thanku baptiste
+ 1
You are welcomed :)