An Interest In:
Web News this Week
- April 27, 2024
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
January 4, 2021 04:41 pm GMT
Original Link: https://dev.to/aswin2001barath/bitwise-operators-in-python-4pii
Bitwise Operators in python
When it comes to binary numbers, bitwise operators are the choice.
Bitwise operators are used to perform operations on binary numbers.
AND, OR, XOR operators
- AND
&
operator sets each bit to 1 if both bits are 1. - OR
|
operator sets each bit to 1 if one of two bits is 1. - XOR
^
operator sets each bit to 1 if only one of two bits is 1.Output:
AND 82OR 2039XOR 1957
Enter fullscreen mode Exit fullscreen mode
Ha Ha, surprised about the outputs?!
The outputs are a result of the binary numbers a and b which gets converted into an integer, each time bitwise operation is performed.
NOT operator
- NOT
~
operator inverts all the bits. - In python, the number gets converted into an inverted signed number.Output:
NOT -11
Enter fullscreen mode Exit fullscreen mode
Shift operators
- left shift
<<
operator shifts left by pushing zeros in from the right and let the leftmost bits fall off. - right shift
>>
operator shifts right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off.Output:
Right shift 277Left shift 4444
Enter fullscreen mode Exit fullscreen mode
Code along and have fun ;)
Original Link: https://dev.to/aswin2001barath/bitwise-operators-in-python-4pii
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To