Bitwise Operators in Python in Hindi

Arpit Nageshwar
⏰ 4 min read

Bitwise Operators in Python in Hindi – Bitwise Operators Kya Hai?

Table of Contents

Introduction to Bitwise Operators in Hindi – Bitwise Operators Kya Hai?

  • अब तक हमने जितने भी operators पढ़े हैं (Arithmetic, Relational, Logical), वो सब numbers या values को पूरी तरह से (as a whole) लेकर operate करते हैं, लेकिन Bitwise Operators इनसे बिल्कुल अलग तरीके से काम करते हैं।

  • Simple भाषा में कहें तो Bitwise Operators वह operators होते हैं जो किसी number की पूरी value पर नहीं, बल्कि उसके individual bits (0 और 1) पर सीधे operate करते हैं।

  • Computer के अंदर हर number बाइनरी (0 और 1) के रूप में store होता है, जैसे 10 को binary में 1010 लिखा जाता है, Bitwise Operators इन्हीं individual bits को एक-एक करके compare या modify करते हैं।

  • जैसे बिजली के switches की एक row हो, जिसमें हर switch या तो ON (1) हो या OFF (0), Bitwise Operators उन्हीं switches पर काम करते हैं, यानी हर bit को individually control किया जाता है।

  • Bitwise Operators को इस्तेमाल करने से पहले number को binary में convert करना पड़ता है, हालांकि Python में यह काम internally अपने आप हो जाता है, programmer को manually binary में convert करने की जरूरत नहीं पड़ती।

  • यह operators normal programming में उतने ज्यादा इस्तेमाल नहीं होते, लेकिन Low-Level Programming, Networking, Cryptography और Performance Optimization जैसी जगहों पर यह बेहद जरूरी हो जाते हैं।

  • Exam की नज़र से देखा जाए तो Bitwise Operators का मतलब है – "wo operators jo kisi number ke individual bits (0 aur 1) par directly operate karte hain, na ki uski poori decimal value par."

Types of Bitwise Operators in Hindi – AND, OR, XOR, NOT, Left Shift, Right Shift

Bitwise Operators ke Types Bitwise Operators AND ( & ) Dono bits 1 hon to 1 OR ( | ) Koi ek bit 1 ho to 1 XOR ( ^ ) Bits alag hon to 1 NOT ( ~ ) Bits ko invert karta hai Left Shift ( << ) Bits ko left shift karta hai Right Shift ( >> ) Bits ko right shift karta hai

Example ke liye a = 12 (1100) aur b = 10 (1010) le lete hain:

  • 1. Bitwise AND (&):
    यह operator दोनों numbers के हर bit को compare करता है, और result में सिर्फ वही bit 1 आता है जहां दोनों numbers की bit 1 हो।

    a = 12   # 1100
    b = 10   # 1010
    print(a & b)   # Output: 8   (1000)
    
  • 2. Bitwise OR (|):
    यह operator दोनों numbers के bits को compare करता है, और result में वह bit 1 आता है जहां कम से कम एक number की bit 1 हो।

    print(a | b)   # Output: 14   (1110)
    
  • 3. Bitwise XOR (^):
    यह operator तभी 1 return करता है जब दोनों numbers की उस position पर bits अलग-अलग हों (एक 0 और दूसरी 1), अगर दोनों bits same हों तो result 0 आता है।

    print(a ^ b)   # Output: 6   (0110)
    
  • 4. Bitwise NOT (~):
    यह एक unary operator है (यानी सिर्फ एक ही operand लेता है), यह हर bit को invert (0 को 1 और 1 को 0) कर देता है, Python में इसका result -(n + 1) के बराबर आता है।

    print(~a)   # Output: -13
    
  • 5. Left Shift (<<):
    यह operator number के सारे bits को दी गई संख्या जितना left की तरफ shift कर देता है, और खाली जगहों में 0 भर देता है, इसका effect number को 2 की power से multiply करने जैसा होता है।

    print(a << 2)   # Output: 48   (12 * 2^2 = 48)
    
  • 6. Right Shift (>>):
    यह operator number के सारे bits को दी गई संख्या जितना right की तरफ shift कर देता है, इसका effect number को 2 की power से divide (floor division) करने जैसा होता है।

    print(a >> 2)   # Output: 3    (12 / 2^2 = 3)
    

Quick Summary Table (a = 12, b = 10):

Operator Naam Example Result
&Bitwise ANDa & b8
|Bitwise ORa | b14
^Bitwise XORa ^ b6
~Bitwise NOT~a-13
<<Left Shifta << 248
>>Right Shifta >> 23

Applications of Bitwise Operators in Hindi – Bitwise Operators कहां इस्तेमाल होते हैं?

  • 1. Checking Even or Odd Number:
    किसी number की सबसे आखिरी bit को & 1 से check करके, बहुत तेज़ी से यह पता लगाया जा सकता है कि number Even है या Odd, यह तरीका Modulus (%) operator से भी fast होता है।

    num = 15
    if num & 1 == 1:
        print("Number Odd hai")
    else:
        print("Number Even hai")
    
  • 2. Fast Multiplication/Division by Powers of 2:
    Left Shift और Right Shift की मदद से किसी number को 2, 4, 8 जैसी powers of 2 से बहुत जल्दी multiply या divide किया जा सकता है, यह normal * या / operator से भी fast होता है।

  • 3. Setting, Clearing aur Toggling Bits:
    Networking aur Embedded Systems में अक्सर किसी particular bit को set (1 करना), clear (0 करना) या toggle (उल्टा करना) करना पड़ता है, यह सब काम AND, OR, XOR operators की मदद से किया जाता है।

  • 4. Data Encryption aur Cryptography:
    XOR operator का इस्तेमाल simple encryption algorithms में बहुत ज्यादा किया जाता है, क्योंकि एक ही key से data को दो बार XOR करने पर original data वापस मिल जाता है।

  • 5. Permissions aur Flags Manage Karna:
    Operating Systems और Networking Protocols में अक्सर अलग-अलग permissions (जैसे Read, Write, Execute) को अलग-अलग bits के रूप में store किया जाता है, और Bitwise Operators से इन्हें आसानी से check या modify किया जाता है।

  • 6. Graphics aur Image Processing:
    Images में color values (RGB) को bits के रूप में store किया जाता है, इसलिए masking aur blending जैसे operations करने के लिए Bitwise Operators का इस्तेमाल होता है।

  • 7. Competitive Programming:
    Bitwise Operators की मदद से कई algorithms (जैसे subsets निकालना, unique element ढूंढना) को बहुत कम time complexity में solve किया जा सकता है, इसलिए यह competitive programming में काफी popular हैं।

Difference Between Logical and Bitwise Operators in Python in Hindi

  • Exam में अक्सर यह सवाल पूछा जाता है कि Logical Operators (and, or, not) और Bitwise Operators (&, |, ~) में क्या फर्क है, क्योंकि दिखने में यह दोनों थोड़े मिलते-जुलते लगते हैं।

  • Logical Operators हमेशा पूरी condition (या value) को एक Boolean (True/False) के रूप में treat करते हैं, यानी यह check करते हैं कि पूरी value truthy है या falsy, individual bits से इनका कोई लेना-देना नहीं होता।

  • Bitwise Operators हमेशा number के individual bits पर operate करते हैं, यानी यह हर bit को अलग-अलग compare करते हैं, इनका इस्तेमाल Boolean values के अलावा पूरे integers पर भी होता है।

a = 5    # Binary: 0101
b = 3    # Binary: 0011

print(a and b)   # Output: 3   (Logical: dono truthy hain, isliye last value return hoti hai)
print(a & b)     # Output: 1   (Bitwise: 0101 & 0011 = 0001)

print(a or b)    # Output: 5   (Logical: pehli truthy value return hoti hai)
print(a | b)     # Output: 7   (Bitwise: 0101 | 0011 = 0111)

Quick Comparison Table:

Point Logical Operators Bitwise Operators
Symbolsand, or, not&, |, ^, ~, <<, >>
Kaam Karte HainPoori value (truthy/falsy) parIndividual bits (0, 1) par
Result TypeTrue/False ya operand ki valueHamesha ek number (integer)
Short-Circuit EvaluationHoti hai (jaise a and b mein agar a False ho to b check hi nahi hota)Nahi hoti, dono operands hamesha evaluate hote hain
Use CaseConditions check karne ke liye (if, while)Bit-level manipulation, performance optimization ke liye

Exam में याद रखने का सबसे आसान तरीका यह है – Logical Operators पूरी value को True या False की तरह देखते हैं, जबकि Bitwise Operators उसी value के अंदर छुपे 0 और 1 के pattern के साथ काम करते हैं।

Frequently Asked Questions (FAQ) – Bitwise Operators in Python in Hindi

वह operators जो किसी number की individual bits (0 और 1) पर सीधे operate करते हैं, न कि उसकी पूरी decimal value पर, उन्हें Bitwise Operators कहते हैं।

Python में कुल 6 मुख्य Bitwise Operators होते हैं – AND (&), OR (|), XOR (^), NOT (~), Left Shift (<<) और Right Shift (>>)।

Python में Bitwise NOT (~) का result हमेशा -(n + 1) के बराबर आता है, क्योंकि यह number को उसके two's complement representation में invert करता है।

Logical Operators (and, or, not) पूरी value को True/False की तरह evaluate करते हैं, जबकि Bitwise Operators (&, |, ^, ~) number की individual bits पर operate करते हैं।

Left Shift (<<) और Right Shift (>>) का सबसे common इस्तेमाल किसी number को बहुत तेज़ी से 2 की power से multiply या divide करने के लिए किया जाता है।

Arpit Nageshwar

✍️ Arpit Nageshwar

Post-graduated | Web Developer | +3 yr Experience | IIT Kharagpur Certified