+ 3

can't understand this challenge

hi x = 3 print(x >> 2) the output is 0 how they got zero!? thank you

4th Oct 2017, 5:12 PM
Mohammed Chami
Mohammed Chami - avatar
6 Answers
+ 8
It's a bitshift operation - it acts on binary representations of numbers. 3 is binary 11. If you shift 2 bits rightwise, it goes to 0. 0011 --1-shift-right--> 0001 --another-shift--> 0000
4th Oct 2017, 5:18 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 8
5>>2 is 1. 5>>3 is 0. Once it goes down to 0 it stays there, so 5>>20 is 0 :)
4th Oct 2017, 5:33 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 6
bitshift division works like integer division by powers of 2: that is x>>1 is equivalent to x//2; x>>2 is equivalent to x//4; x>>3 is equivalent to x//8; ... x>>10 is equivalent to x//(2**10) and so on... on the other side x<<1 is equivalent to x*2; x<<2 is equivalent to x*4; x<<3 is equivalent to x*8, and so on.. hence 3>>2 is 3//4=0 5>>2 means 5//4=1...
4th Oct 2017, 5:41 PM
m abrate
m abrate - avatar
+ 4
@kuba what if x is equal to 5?
4th Oct 2017, 5:21 PM
Mohammed Chami
Mohammed Chami - avatar
+ 3
in binary 3 is written as 0011.In program there is x=3 and x>>2 means shift 3 two times right so when u shift this, in binary u will get 0000 in binary which is equal to 0. if it will be x>>1 then in binary it will be 0001 which is equal to 1. for more example let us take x=20 in binary 10100, then x>>1 will be 01010 i.e 10 and x>>2 will be 00101 i.e 5 and x>>3 it will be 00010 i.e 2 and so on. hope u understand.
4th Oct 2017, 5:23 PM
Sudhanshu
0
Its mistake, the output must be 3.
4th Oct 2017, 5:16 PM
ya_Bob_Jonez