+ 1

n&=n << 1 in python means?

See during Python problem solving

12th Sep 2021, 2:06 PM
Dipanshu 👑
Dipanshu 👑 - avatar
3 odpowiedzi
+ 4
suppose n=5, n binary representation is "00101" operators "<<" and "&" operate on bits that's why we are looking at them in their binary form. n&=n<<1 is equal to , n=n & n << 1 << has higher precedence than & so it will be evaluated first. "<<" Is a left shift operator which shifts the bits by some places which is decided by the number following the operator. Here n<<1 will shift the bits to left by 1 place and so our new binary representation will look like "01010" which in decimal is equal to 10. now we are left n=n & 10 & operator will match the bits of two operands and yield a new bit . So 1 & 0 will return 1 , 1 & 1 will return 1 , 0 & 0 will return 0. when we apply & on both binary numbers it will look like this, 00101 01010 ----------- 00000 00000 is 0 in decimal.
12th Sep 2021, 2:27 PM
Abhay
Abhay - avatar
+ 1
Abhay Thanks for such an appreciable explanation I got it And I think you by mistake write it 1&0 will return 1 It will returns 0 I know you know but aise hi 😁
12th Sep 2021, 2:43 PM
Dipanshu 👑
Dipanshu 👑 - avatar
0
Dipanshu 👑 lol , thks for correction :)
12th Sep 2021, 3:07 PM
Abhay
Abhay - avatar