Python Bitwise Operators

Python Bitwise Operators

In general, Python operators are often used to handle values and arguments. This tutorial will explore a specific form of Python operator called bitwise operators. These symbols indicate conventional symbols for mathematical and logical procedures.

Bitwise Operators

Bitwise operations are processes that engage with individual bits, which are the fundamental constituent of any sort of data in a computer. A binary value of 0 or 1 is assigned to each bit. Regardless of the notion that machines can manipulate bits, machines usually store data and perform commands in bytes, which are bit multiples. Most scripting languages work with groups of 8 bits, 16 bits, or 32 bits.

Bitwise operators are characters that denote operations that are performed on single bits. A bitwise operation is executed by spatially aligning the distinct bits of two-bit patterns of the same size.

Bitwise Operators in Python

Bitwise operators are employed in python to perform bitwise operations on numbers. The values are first converted to binary, and then manipulations are done bit by bit, hence the phrase “bitwise operators.” The outcome is then displayed in decimal numbers.

Bitwise Logical Operator: These operators are employed to execute logical operations like other logical operators bit by bit. This is akin to using logical operators such as and, or, and not on a bit level. Except for these basic facts, bitwise and logical operators are analogous.

Bitwise Shift Operators: These operators multiply or divide an integer number by two by shifting every individual bit to the left or right. We can use them when we wish to multiply or divide a value by a power of 2. Python’s bitwise operators only work with integers.

Operator Description
& Binary AND TThe operator sends the bit present in both operands to the output.
| Binary OR TIf a bit is present in either operand, it is copied.
^ Binary XOR TIt is copied if the bit is set inside one argument but not both.
~ Binary Ones Complement TIt has the function of ‘flipping’ bits and is unary.
<< Binary Left Shift TThe left operand’s value is moved to its left by the number of bits specified in the right argument.
>> Binary Right Shift The quantity of bits provided by the right parameter advances the position of the left operand.

Every binary bitwise operator has a compound operator that performs an enhanced application.

Operator Syntax Equivalent to
&= N1 &= N2 N1 = N1 & N2
|= N1 |= N2 N1 = N1 | N2
^= N1 ^= N2 N1 = N1 ^ N2
<<= N1 <<= n N1 = N1 << n
>>= N1 >>= n N1 = N1 >> n

These are rules for updating the left-hand argument whilst it’s still active.

Examples of Bitwise Operators in Python

Bitwise AND

The logical conjunction is performed by the bitwise AND operation (&) on the appropriate bits of the provided operands.

Code

Output:

x & y = 0

Bitwise OR

Logical disjunction is achieved using the bitwise OR operation (|). If, at minimum, one of the appropriate pair of bits is turned on, it yields a one.

Code

Output:

x | y = 19

Bitwise NOT

Because the bitwise NOT operator only requires one parameter, this is the only unary bitwise operator. It flips all of the bits of a number given to implement logical negation upon it.

Code

Output:

~x = -44

Bitwise XOR

When one of the bits is 1, another is 0, it returns true; otherwise, it returns false.

Code

Output:

x ^ y = 10

Bitwise Right Shift

As a result of this operation, the individual bits of the number are shifted to the right, and the gaps on the left are filled with 0 (or 1 if the number is negative). The result is similar to dividing a value by a power of 2.

Code

Output:

x >> 1 = 13
y >> 1 = -13

Bitwise Left Shift

As a result of the operation, the individual bits of the integer to the left are filled with 0 on the voids to the right. The result is similar to multiplying a value by a power of 2.

Code

Output:

x << 1 = 104
y << 1 = -30

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263136.html

(0)
上一篇 2022年5月30日
下一篇 2022年5月30日

相关推荐

发表回复

登录后才能评论