Important Notice:

Bitwise Operators

Bitwise Operators

28 views 3 min read

Bitwise Operators (बिटवाइज़ ऑपरेटर) :-

Bitwise Operators (बिटवाइज़ ऑपरेटर) वे Operators हैं जिनका उपयोग integers की individual bits (0 और 1) पर operations करने के लिए किया जाता है।

Python में Bitwise Operators किसी number को उसके Binary Form (द्विआधारी रूप) में process करते हैं।

English

Bitwise Operators are used to perform operations on the individual bits of integer values. They work directly with the binary representation (0 and 1) of numbers.

Bitwise Operators are mainly used in low-level programming, data processing, flags, masking, and system-level operations.

Syntax -

operand1 operator operand2
Example -
a = 10
b = 6
print(a & b)
 
Binary Representation -
10 = 1010
 6 = 0110
-----------
& = 0010
 
Output -
2
 
1. Bitwise AND Operator (&) :-

The Bitwise AND Operator (&) compares corresponding bits of two numbers.

Bitwise AND Operator (&) दो numbers की corresponding bits की तुलना करता है।

यदि दोनों bits 1 हैं, तो result 1 होता है, अन्यथा 0 होता है।

Truth Table -

A   B   A & B
0   0   0
0   1   0
1   0   0
1   1   1
 
Example -
a = 10
b = 6
print(a & b)
 
Binary Representation -
10 = 1010
 6 = 0110
-----------
& = 0010
 
Output -
2
 
2. Bitwise OR Operator (|) :-

The Bitwise OR Operator (|) compares corresponding bits of two numbers.

Bitwise OR Operator (|) में यदि किसी भी position पर कम से कम एक bit 1 हो, तो result 1 होता है।

Truth Table -
A   B   A | B
0   0   0
0   1   1
1   0   1
1   1   1
 
Example -
a = 10
b = 6
print(a | b)
 
Output -
14
 
Binary Representation -
10 = 1010
 6 = 0110
-----------
| = 1110
 
 
3. Bitwise XOR Operator (^) :-

The Bitwise XOR Operator (^) returns 1 when the corresponding bits are different.

Bitwise XOR Operator (^) में दोनों bits अलग होने पर result 1 और समान होने पर 0 होता है।

Truth Table -
A   B   A ^ B
0   0   0
0   1   1
1   0   1
1   1   0
 
Example -
a = 10
b = 6
print(a ^ b)
 
Output -
12
 
4. Bitwise NOT Operator (~) :-

The Bitwise NOT Operator (~) reverses each bit of a number.

Bitwise NOT Operator (~) प्रत्येक bit को उलट देता है:

0 → 1
1 → 0
 
Python में negative numbers के कारण ~ का result समझने के लिए यह rule महत्वपूर्ण है:
 
~n = -(n + 1)
 
Example -
a = 10
print(~a)
 
Output -
-11
 
Calculation -
~n = -(n + 1
~10 = -(10 + 1)
    = -11
 
5. Left Shift Operator (<<) :-

The Left Shift Operator (<<) shifts the bits of a number to the left by the specified number of positions.

Left Shift Operator (<<) bits को left side में shift करता है।

Example -
a = 10
print(a << 2)
 
Working -
10 = 1010

1010 << 2
= 101000
 
101000 = 40
 
Output -
40
Important: Positive numbers के लिए a << n सामान्यतः a × 2ⁿ के बराबर होता है।
 
6. Right Shift Operator (>>) :-

The Right Shift Operator (>>) shifts the bits of a number to the right by the specified number of positions.

Right Shift Operator (>>) bits को right side में shift करता है।

Example -
a = 20

print(a >> 2)
 
Working -
20 = 10100

10100 >> 2
= 00101
00101 = 5
 
Output -
5

Important: Positive numbers के लिए a >> n सामान्यतः a // 2ⁿ के बराबर होता है।
 

Related Notes