What are bitwise operators in python? |types of bitwise operators

we all know some of the operators like arithmetic operators logical operators and now we are going to see bitwise operators

firstly, bitwise operators, we will deal with the bits by these operators we can operate the things like bit array, bit string, or bit numerals.

Secondly, we can use these operators as they are fast In their action and in a simple manner

Coming to the bitwise operator we are having a totally 6 different operators


Operators

Symbols
Complement
~
Or
|
And &
XOR^
Left shift<<
Right shift>>
these are the bitwise operators

Complement operator

Here we are using the symbol called ~ TILDE

now let’s try what this operator can do

To understand this we must know about binary operations with this complementory operators

Generally complement means opposite of it 
so if we 
~1 will gives 0
~0 will gives 1 

Example

a=15
print(~a)
print(bin(a))

Output

-16
0b1111

Explanation

For 15 we are getting  -16 how?
     binary format of 15 is     00001111

     its comlpement will be   11110000 which will be equal to -16 

Is a negative number possible in python? yeah, we can have negative numbers but we should store it as positive numbers,

now how can we do that, there is a concept called as

2’s complement

we can find 2’s complement by finding 1’s complement and add 1 to it

for example we will take 2's complement for number 13 
13 in binary can be written as 00001101
its complement will be            1111001
for 2's complement                          +1      
                                             ------------
                                                11110011         which will be equal to 12

Bitwise And operator

And operator will give 1 (true) when both the inputs are 1 (true)

we can understand it with the help of the truth table

Here for the bitwise and we are using & (AMPERSAND) Symbol

Example

print(12&13)

Output

12

Now why are we getting 12 as output

to understand that we need to convert into binary format

 
12 in binary can be written as 00001100
13 in binary can be written as 00001101    
                                           --------------
                                             000001100    which will be equal to 12

that’s why we are getting 12 as output

Bitwise OR operator

And operator will give 1 (true) when any one of the inputs are 1 (true)

we can understand it with the help of the truth table

Here for the bitwise and we are using | (pipe) Symbol

Example

print(12|13)

Output

13

Now why are we getting 13 as output

to understand that we need to convert into binary format

 
12 in binary can be written as 00001100
13 in binary can be written as 00001101    
                                           --------------
                                                00001101    which will be equal to 13

that’s why we are getting 13 as output

There is no condition that the output must be one of the given input it can also be any number other than the given numbers

Example

print(45&62)

Output

44

Explanation

 
45 in binary can be written as 00101101
62 in binary can be written as 00111110    
                                           --------------
                                                00101100    which will be equal to 44

to know more about other operators

  1. XOR bitwise operator
  2. left shift bitwise operator
  3. rightshift bitwise operator
We will be happy to hear your thoughts

Leave a reply

Edusera
Logo
Open chat
1
Scan the code
Hello!๐Ÿ‘‹
Can we help you?