Skip to main content

Bit wise and Shift Operators

 It is used to perform the operations on bits (0 or 1).

Symbol

Operator Name

&

|

^

~

<< 

>> 

Bitwise and

Bitwise or

Bitwise xor

Bitwise compliment

Left shift

Right shift

Suppose a=10,b=20

And: In this case all are true then it returns true otherwise it returns false.

Or: At least one is true then it returns true otherwise it returns false.

Exclusive Or (XOR): One condition is true it returns true otherwise it return false.

Compliment: It performs reverse action

            True->False

            False->True

Truth Table

a

b

a&b

a|b

a^b

1

1

1

1

0

1

0

0

1

1

0

1

0

1

1

0

0

0

0

0

Formulas of left and right shift operators

 a<<b =>       a*2b

a>>b  =>       a/2b

The binary values of a and b is

a          =          01010=10

b          =          10100=20

            a&b    =          00000=0

            a|b     =          11110=30

            a^b     =          11110=30

            ~a       =          10101=-11

            4<<3  =          4*23=32

            4>>3  =          4/23=4/8=0

Program

a=10

b=20

print(a&b)

print(a|b)

print(a^b)

print(~a)

print(~b)

print(4<<3)

print(4>>3)

Output

0

30

30

-11

-21

32

0

 

Comments