Different Types of Bitwise Operators And Usage

In this tutorial, we will learn different types of Bitwise operations and usage where we will see the use of different bitwise operators in python.

Bitwise

Bitwise Operators

There are different Bitwise operators which we will discuss here with their examples and some explanation and six of them are listed here with some descriptions.

‘&’

It is called the ‘AND’ operator which performs between two values and it returns the value when each but is 1 only otherwise it returns zero. or we can say the output of it is one only when we have both the digits are one otherwise zero.

‘|’

It is the ‘OR’ operator which is nothing it is one where if there is anyone there in two digits and it gives 0 when both are zero. for understanding in a better way follow the example given below.

‘^’

It is called an ‘XOR’ operator which is also called an exclusive OR which is used for returning a value each bit is 1 only if the corresponding bits in the input values are different.

‘~’

It is nothing but a ‘NOT’ operator which has the functionality to reverse the digit from zero to one and one to zero.

‘<<‘

The Left Shift operator is used for sifting digits for a given number of places it is used with some arguments and the digit is being shifted those number of times to the left.

‘>>’

As we have left the shift It is the Right Shift that has the same functionality fo shifting the digits but in this case, it is used for shifting the digits on the right side as per the given number of times in the arguments.

Here we have taken an example of using all the explained operators as a code to show the working of each operator with a live example.

a = 0b10101010   # binary representation of 170
b = 0b11110000   # binary representation of 240

# bitwise AND
c = a & b       # binary representation of 160 (0b10100000)

# bitwise OR
d = a | b       # binary representation of 250 (0b11111010)

# bitwise XOR
e = a ^ b       # binary representation of 90 (0b01011010)

# bitwise NOT
f = ~a          # binary representation of -171 (0b...11101010)

# left shift
g = a << 2      # binary representation of 680 (0b1010101000)

# right shift
h = b >> 3      # binary representation of 30 (0b00011110)

 

To learn more about different types of Bitwise operations and usage you may visit: Bitwise Operators with some examples.

To learn more about python programming and solutions for the problems faced in solving python programming problems and even for concepts we have tutorials so visit: Python Tutorials And Problems.

 

To learn more about different programming languages and the solutions along with concepts tutorials visit: beta python programming languages Solutions.

Leave a Comment

%d bloggers like this: