Bitwise Operator Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 12

Bitwise Operator Python

Anyone explian with example how right shift , left shift and ZERO-FILL RIGHT SHIFT works! (<< ,>> and <<<) in python . https://code.sololearn.com/WBOdrCZZT0a7/?ref=app

18th Apr 2021, 3:43 AM
Cheenu💕™
Cheenu💕™ - avatar
7 Answers
+ 8
Bitwise left shift: Shifts the bits of the number to the left and fills 0 on voids left as a result. Similar effect as of multiplying the number with some power of two. Bitwise right shift: Shifts the bits of the number to the right and fills 0 on voids left as a result. Similar effect as of dividing the number with some power of two. https://code.sololearn.com/c9Dhz8R0K2hD/?ref=app
18th Apr 2021, 3:59 AM
Kǟrɨsɦmǟ ❥
Kǟrɨsɦmǟ ❥ - avatar
+ 7
Cheenu💕™ There is not a built-in zero fill right shift operator in Python, but you can easily define your own zero_fill_right_shift function: def zero_fill_right_shift(val, n): return (val >> n) if val >= 0 else ((val + 0x100000000) >> n) Then you can define your function: def f(e, t): return e << t or zero_fill_right_shift(e, 32 - t)
18th Apr 2021, 4:24 AM
Kǟrɨsɦmǟ ❥
Kǟrɨsɦmǟ ❥ - avatar
+ 6
Vadivelan already seen burey's post! ✨Kårï§hmå✨ and what about ZERO-FILL RIGHT SHIFT (<<<)
18th Apr 2021, 4:09 AM
Cheenu💕™
Cheenu💕™ - avatar
+ 3
https://www.interviewcake.com/concept/java/bit-shift
18th Apr 2021, 3:56 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Zero fill right shift >>> not <<< (arrows will point toward the direction of shift) Also known as Logical Right Shift. The link I provided covers all 3 that you asked about and explains them well. Please read and if you have a more specific and explicit question afterwards, please feel free to ask it. Doesn't matter that it is Java, works the same for most languages, including Python.
18th Apr 2021, 4:14 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
This explains in pretty good detail as to why Python doesn't have a zero fill right shift and how to work around it using similar techniques to what ✨Kårï§hmå✨ has posted. https://realpython.com/python-bitwise-operators/
18th Apr 2021, 5:36 AM
ChaoticDawg
ChaoticDawg - avatar