What are Bitwise Operators in Python

 Python Bitwise Operators in hindi

Bitwise Operators

Bitwise operators are used to compare (binary) numbers:

Operator

Name

Description

AND

Sets each bit to 1 if both bits are 1

|

OR

Sets each bit to 1 if one of two bits is 1

 ^

XOR

Sets each bit to 1 if only one of two bits is 1

NOT

Inverts all the bits

<< 

Zero fill left shift

Shift left by pushing zeros in from the right and let the leftmost bits fall off

>> 

Signed right shift

Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off

Truth Table

Var X

Var Y

& (And)

| (Or)

^ (XOR)

~ (Not)

0

0

0 (False)

0 (False)

0 (False)

1

0

1

0 (False)

1 (True)

1 (True)

1

1

0

0 (False)

1 (True)

1 (True)

0

1

1

1 (True)

1 (True)

0 (False)

0

 

 

 

 

 

X complement

Value of X

X<<1

X>>1

 

 

 

X = 0011

0110

1001

 

 

 

Action

1 step left shift

1 step right shift

 

 

 

 

Esamples:

X = 10

Y = 8

First, we calculate binary of these variables, so use function like:

Print (bin(x))

Print (bin(y))

Output:                                0b 1010

                                0b 1000

Now use bitwise (&) operator

Print (x & y, bin(x & y))

Output:                                8  0b 1000

We will use bitwise (|) operator on same value:

Print (x | y, bin(x | y))

Output:                                10  0b 1010

Now we will use bitwise (|) operator on same value:

Print (x ^ y, bin(x ^ y))

Output:                                2  0b 10

Now we will use bitwise (<<) operator on same value:

Print (x << 1, bin(x << 1))

Output:                                20  0b 10100

Now we will use bitwise (>>) operator on same value:

Print (x >> 1, bin(x >> 1))

Output:                                5  0b 101

Now we will use bitwise (~) operator on same value:

Print (~ x, bin( ~ x))

Output:                                -11  0b 1011


<<Previous                                                  Next>>

कोई टिप्पणी नहीं

टिप्पणी: केवल इस ब्लॉग का सदस्य टिप्पणी भेज सकता है.

Send Multiple Emails From Excel | Send Bulk Mail from Excel Sheet with Attachment in One Click

Send Multiple Emails From Excel | Send Bulk Mail from Excel Sheet with Attachment in One Click Download VBA Code Notepad file - Click here D...

Blogger द्वारा संचालित.