Logical and Bitwise Operators in Python

In this class, we discuss logical and bitwise Operators in pyton.

For Complete YouTube Video: Click Here

Logical Operators in Python

First, to understand the concept of logical operators we should have some basic knowledge of number systems and boolean algebra.

In logical operators, we have And, Or, and Not.

Logical And means both the values are True then only Logical And Gives True.

X Y And Or

T T T T

T F F T

F T F T

F F F F

From the above table, T means True, and F means False.

And from the above table shows that both x and y are true, then And is True.

Or from the above table shows that any one of x and y is True, then Or is True.

X Not X

T F

F T

Not from the above table shows the negation of X value.

If X is True, Not X is False, and vice-versa.

Take an example and understand Logical operators.

x=5, y=6, z=7

print(x<y and y<z) will display True.

As x<y is the comparison operator which gives True, and y<z also gives True.

Both comparison operators are giving True, so And logical operator is giving True value.

Bitwise Operators

Let’s take an example and understand Bitwise operators

a=10 is written in binary as 00001010->least significant bit

b=7 is written in binary as 00000111->least significant bit

For the understanding purpose, we have taken 8bits in binary.

The symbol for Bitwise and is &.

Print(a&b) will display 2 ie in binary 00000010.

How bitwise and operation is done?

Take bits from both variables from the least significant bit to the most significant bit.

Take least significant bits from both the variables, on those two bits do and operation.

bitwise and means if both bits are 1 then only output 1.

Continue and operation on the next bit from both the variables and so on.

Bitwise or: The symbol for bitwise or is |.

Bitwise or is 1 if at least one bit is 1 then bitwise or operation returns 1.

print(a|b) will display 00001111 in decimal 15.

Exclusive or: The symbol for bitwise Exclusive or is ^.

Exclusive or value will be 1 if both the bits are different ie one bit is 1 and the other bit is 0.

print(a^b) will display 00001101 in decimal 13.

Bitwise Not: The symbol for bitwise not is ~.

Bitwise not will complement the bits. ie if 1 is there it will place 0 and vice versa.

print(~a) will display 11110101. we have given here in binary.

Take each bit of variable a, and complement it.

Left Shift: The symbol for the left shift is <<.

Take a = 5. In binary, it is given as 00000101.

The left shift will do a logical shift operation. How logical shift works?

Logical shift place 0 in the least significant position and the bit in the least significant position moves to the next position. like this, each bit position is shifted one bit.

print(a<<1) will display 00001010 which is decimal equivalent to 10.

The value obtained after the left shift is double the initial value.

print(a<<2) means do left shift 2 times. The value displayed is 20.

Right shift: The symbol for the right shift is >>.

The right shift will do the Arithmetic shift operation.

How does arithmetic shift operation work?

Take example a= -10. in the binary form it is most significant bit<-11110110.

In arithmetic right shift most significant bit is placed as it is.

Then most significant bit is placed in the next position and so on shift bit positions.

print(a>>1) will result 11111011 which is decimal equivalent -5.

The above example shows if we did the right shift our value is getting divided by 2.

Take a=-9 if we do the right shift we get the value -5. means here it’s taking ceil value of 4.5.