+ 3

Please explain this question???

#include <iostream> using namespace std; int main() { int x=17; cout << (x>>2); } why the output is 4???

13th Aug 2017, 7:09 PM
Sachin Dubey
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
13th Aug 2017, 7:26 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
13th Aug 2017, 7:21 PM
Manual
Manual - avatar
+ 1
the output is 68 "x << 2" is a bit wise operation = 2^2 = 4
13th Aug 2017, 7:17 PM
Manual
Manual - avatar
+ 1
thanku baptiste
13th Aug 2017, 7:32 PM
Sachin Dubey
+ 1
You are welcomed :)
13th Aug 2017, 7:32 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar