what are bitwise operators? XOR | Left shift | Right shift operators

we have seen Some of the bitwise operators like Complement, AND, OR

still, there are 3 bitwise operators to know


Operators

Symbols
Complement~
Or
|
And
&
XOR
^
Left shift<<
Right shift>>

XOR operator

For the XOR operator, we are using the symbol called Cap ^

The truth table for the XOR operator

Example

print(25^30)

Output

7

Understanding the XOR operator

here we can see what the XOR operator is doing

When both of the 2 inputs are the same we will get 0 (FALSE) as output

  1. Case 1 (SAME)
    1. if both the inputs are 0 and 0 We will get the output as 0
    2. if both the inputs are 1 and 1 We will get the output as 0
  2. Case 2 (Different)
    1. if one of the input is 1 and other is 0 We will get the output as 1
    2. if one of the input is 0 and other is 1 We will get the output as 1

Explanation

here we are printing value by performing the XOR operator for the numbers 25 and 30

and we are getting the output as 7

So what’s happening behind the code?

To understand that we need to know about the bit operations

if you don’t know check it out here

Behind the code

25 in binary can be written as 00011001
30 in binary can be written as 00011110     By performing XOR operations we will get 
                                           --------------   
                                                00000111    which will be equal to 7

this is why we are getting 7 as output ✔

Left Shift operator

For the Left Shift operator, we are using the symbol called Double lesser than symbol <<

Source: https://blog.finxter.com/python-bitwise-left-shift/

Example

print(8<<1)
print(8<<2)

Output

16
32

Understanding the Left shift operator

here we can see what the Left shift operator is doing

Syntax

Number << number of Shifting

Explanation

  1. Case 1 (Number of shift = 1)
    1. Firstly Convert number into binary form
    2. Then for left shift Add one zero bit to right side at left most bit (LSB)
    3. then convert that shifted bits into integer form we will get the output
  2. Case 2 (Number of shift = 2 )
    1. Firstly Convert number into binary form
    2. Then for left shift Add two zero bits to right side at left most bit (LSB)
    3. then convert that shifted bits into integer form we will get the output

Explanation

here we are printing value by performing the Left shift operator for the number 8 and performing 1 shift and 2 shifts and we are getting the output as 16 and 32 respectively

Right Shift operator

For the Right Shift operator, we are using the symbol called Double Greater than symbol >>

Understanding the Right shift operator

The right shift operator is similar to that of the left-shift operator that we have seen before

Syntax

Number >> number of Shifting

Explanation

  1. Case 1 (Number of shift = 1)
    1. Firstly Convert number into binary form
    2. Then for left shift remove one bit from Right side at left most bit (MSB)
    3. then convert that shifted bits into integer form we will get the output
  2. Case 2 (Number of shift = 2 )
    1. Firstly Convert number into binary form
    2. Then for left shift Remove two bit from Right side at left most bit (MSB)
    3. then convert that shifted bits into integer form we will get the output
We will be happy to hear your thoughts

Leave a reply

Edusera
Logo
Open chat
1
Scan the code
Hello!👋
Can we help you?