Python Assignment and Bitwise Operators (&, |, ^, ~, <<, >>) Notes in Hindi & English : असाइनमेंट और बिटवाइज़ ऑपरेटर्स: बाइनरी स्तर पर डेटा हेरफेर और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Python Assignment and Bitwise Operators (&, |, ^, ~, <<, >>) (असाइनमेंट और बिटवाइज़ ऑपरेटर्स: बाइनरी स्तर पर डेटा हेरफेर) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: Python assignment operators (=, +=, -=, etc., plus Python 3.8's walrus :=) bind names to objects, while bitwise operators (&, |, ^, ~, <<, >>) manipulate integers directly at the binary bit level. Understanding augmented assignment mutability and Python's infinite-sign-extended two's complement (~x == -(x + 1)) is vital for competitive exams.

अध्याय सार: पायथन असाइनमेंट ऑपरेटर्स (=, +=, -= आदि और 3.8 का वॉलरस :=) नामों को ऑब्जेक्ट्स से बांधते हैं, जबकि बिटवाइज़ ऑपरेटर्स (&, |, ^, ~, <<, >>) पूर्णांकों पर सीधे बाइनरी बिट स्तर पर कार्य करते हैं। म्यूटेबल बनाम इम्यूटेबल ऑगमेंटेड असाइनमेंट और ~x == -(x + 1) का नियम परीक्षाओं के लिए अत्यंत महत्वपूर्ण है।

Python Assignment and Bitwise Operators (&, |, ^, ~, <<, >>): Detailed Notes & Architecture (असाइनमेंट और बिटवाइज़ ऑपरेटर्स: बाइनरी स्तर पर डेटा हेरफेर)

1. Assignment, Augmented Assignment, and the Walrus Operator (:=) (असाइनमेंट, ऑगमेंटेड असाइनमेंट और वॉलरस ऑपरेटर (:=))

[EN] The simple assignment operator (=) is a statement in Python (not an expression, so writing if x = 5: raises SyntaxError) that binds a variable name to an object reference, supporting chained binding (a = b = 0) and tuple unpacking (a, b = b, a). Augmented operators (+=, -=, *=, /=, //=, %=, **=, &=, |=, ^=, <<=, >>=) modify mutable objects like lists in-place via __iadd__ without changing their memory id(), whereas for immutable ints, strings, or tuples they rebind the name to a new object. Introduced in Python 3.8 (PEP 572), the walrus operator (:=) creates assignment expressions that assign and return a value inline, e.g., while (line := input()) != 'quit':.

[HI] साधारण असाइनमेंट (=) पायथन में एक स्टेटमेंट है (एक्सप्रेशन नहीं, इसलिए if x = 5: में SyntaxError आता है) जो चेन्ड असाइनमेंट (a = b = 0) व टपल अनपैकिंग (a, b = b, a) को सपोर्ट करता है। ऑगमेंटेड ऑपरेटर्स (+=, -=, &=, <<= आदि) लिस्ट जैसे म्यूटेबल ऑब्जेक्ट को उसी मेमोरी एड्रेस (in-place) पर बदलते हैं, जबकि int या str जैसे इम्यूटेबल के लिए नया ऑब्जेक्ट बनाकर वेरिएबल को रीबाइंड करते हैं। पायथन 3.8 (PEP 572) में जोड़ा गया वॉलरस ऑपरेटर (:=) एक्सप्रेशन के भीतर ही मान असाइन करने और लौटाने की सुविधा देता है।

2. Bitwise AND (&), OR (|), XOR (^), and NOT (~) (बिटवाइज़ AND (&), OR (|), XOR (^) और NOT (~))

[EN] Bitwise operators act on the binary representation of integers (including bool, since bool subclasses int) and raise TypeError on floats or strings. Bitwise AND (&) yields 1 only if both bits are 1 (ideal for masking bits or testing parity with n & 1); OR (|) yields 1 if either bit is 1 (used to set flags); XOR (^) yields 1 only when bits differ (used to toggle bits or swap two integers via a ^= b; b ^= a; a ^= b). Bitwise NOT (~) flips all bits in an infinite two's complement representation, always obeying the exact identity ~x == -(x + 1), so ~5 == -6 and ~(-1) == 0.

[HI] बिटवाइज़ ऑपरेटर्स पूर्णांकों (और bool) के बाइनरी बिट्स पर कार्य करते हैं तथा float या str पर TypeError देते हैं। & (AND) तभी 1 देता है जब दोनों बिट 1 हों (मास्किंग व सम-विषम जाँच n & 1 में उपयोगी); | (OR) कम से कम एक बिट 1 होने पर 1 देता है (फ्लैग सेट करने हेतु); ^ (XOR) दोनों बिट भिन्न होने पर 1 देता है (बिट टॉगल करने हेतु)। बिटवाइज़ NOT (~) दो के पूरक (two's complement) सिद्धांत पर हर मान के लिए सटीक सूत्र ~x == -(x + 1) का पालन करता है, जैसे ~5 == -6 और ~(-1) == 0।

3. Bitwise Left Shift (<<) and Right Shift (>>) (बिटवाइज़ लेफ्ट शिफ्ट (<<) और राइट शिफ्ट (>>))

[EN] Left shift (x << n) moves all bits of x left by n positions and pads n zeros on the right, mathematically computing x * (2 ** n) without overflow in Python. Right shift (x >> n) performs an arithmetic right shift (preserving the sign bit on negative numbers), which mathematically equals floor division x // (2 ** n) — for example, 20 >> 2 == 5 and -7 >> 1 == -4. Passing a negative shift count such as 8 << -1 or 8 >> -1 raises ValueError: negative shift count.

[HI] लेफ्ट शिफ्ट (x << n) सभी बिट्स को n स्थान बाईं ओर खिसकाकर दाईं ओर शून्य भरता है, जो गणितीय रूप से x * (2 ** n) के बराबर है और पायथन में इसमें ओवरफ्लो नहीं होता। राइट शिफ्ट (x >> n) एरिथमेटिक शिफ्ट है जो ऋणात्मक संख्याओं में साइन बिट सुरक्षित रखता है और x // (2 ** n) के बराबर होता है (जैसे 20 >> 2 == 5 और -7 >> 1 == -4)। यदि शिफ्ट काउंट ऋणात्मक (जैसे 8 << -1) दिया जाए तो ValueError: negative shift count आता है।

4. Operator Precedence Differences from C/Java (C/Java की तुलना में ऑपरेटर प्राथमिकता के प्रमुख अंतर)

[EN] Within Python's precedence hierarchy, bitwise unary ~ sits with unary +/- (below **), followed by binary arithmetic (*, /, +, -), then shift operators (<<, >>), then & (AND), then ^ (XOR), and then | (OR). Crucially, unlike C and Java where == has higher precedence than & (causing bugs in flag & 1 == 1), Python gives &, ^, and | higher precedence than comparison operators (==, !=, <, >). Thus, in Python, 5 & 1 == 1 is cleanly evaluated as (5 & 1) == 1 -> True. Assignment operators (=, +=) and := have the lowest precedence.

[HI] पायथन की प्राथमिकता में ~ (यूनरी +/- के साथ) सबसे ऊपर, फिर अंकगणित (*, /, +, -), फिर शिफ्ट (<<, >>), फिर &, फिर ^ और उसके बाद | आता है। C और Java के विपरीत (जहाँ == की प्राथमिकता & से अधिक होती है), पायथन में &, ^ और | की प्राथमिकता तुलनात्मक ऑपरेटर्स (==, !=, <, >) से अधिक होती है। अतः पायथन में 5 & 1 == 1 सीधे (5 & 1) == 1 के रूप में True देता है। असाइनमेंट (=, +=) और := की प्राथमिकता सबसे कम होती है।

Syntax, Formula & Technical Example (Python Assignment and Bitwise Operators (&, |, ^, ~, <<, >>))

a, b = 12, 10               # 1100, 1010
print(a & b, a | b, a ^ b)  # 8 14 6
print(~12, ~(-1))           # -13 0
print(5 << 2, 20 >> 2)      # 20 5
print(-7 >> 1)              # -4
a ^= 6
print(a)                    # 10

Python Assignment and Bitwise Operators (&, |, ^, ~, <<, >>) Quick Reference & Comparison Table

OperatorBitwise / Assignment MeaningKey Exam Fact
& / | / ^Bitwise AND, OR, XORHigher precedence than == in Python
~Bitwise NOT (1s/2s complement)Always evaluates to -(x + 1)
<< / >>Bitwise Left & Arithmetic Right Shiftx << n == x*(2**n); x >> n == x//(2**n)
+= / :=Augmented & Walrus Assignment+= mutates lists in-place; := is expr

Python Assignment and Bitwise Operators (&, |, ^, ~, <<, >>) Top 20 Bilingual MCQs with Answers

Q1. What is the output of: print(12 & 10) in Python?

(पायथन में print(12 & 10) का आउटपुट क्या होगा?)

(A) 14

(B) 8

(C) 2

(D) 6

Correct Answer: (B)

[EN] 12 in binary is 1100 and 10 is 1010; 1100 & 1010 = 1000, which is 8 in decimal.

[HI] 12 का बाइनरी 1100 और 10 का 1010 है; 1100 & 1010 = 1000 अर्थात दशमलव में 8 होगा।

Q2. What does ~9 evaluate to in Python?

(पायथन में ~9 का मान क्या होगा?)

(A) 6

(B) -10

(C) -9

(D) -8

Correct Answer: (B)

[EN] Python's bitwise NOT obeys ~x == -(x + 1), so ~9 equals -(9 + 1) = -10.

[HI] पायथन में बिटवाइज़ NOT नियम ~x == -(x + 1) का पालन करता है, इसलिए ~9 = -(9 + 1) = -10 होगा।

Q3. What is the value of 12 ^ 10?

(12 ^ 10 का मान क्या है?)

(A) 8

(B) 22

(C) 14

(D) 6

Correct Answer: (D)

[EN] 1100 XOR 1010 gives 0110 (bits differ in positions 2 and 1), which equals 6.

[HI] 1100 XOR 1010 से 0110 प्राप्त होता है, जो दशमलव में 6 है।

Q4. What is the output of: print(3 << 4)?

(print(3 << 4) का आउटपुट क्या होगा?)

(A) 81

(B) 48

(C) 12

(D) 24

Correct Answer: (B)

[EN] Left-shifting 3 by 4 bits computes 3 * (2 ** 4) = 3 * 16 = 48.

[HI] 3 को 4 बिट बाईं ओर शिफ्ट करने पर 3 * (2 ** 4) = 3 * 16 = 48 प्राप्त होता है।

Q5. What does -9 >> 1 evaluate to in Python?

(पायथन में -9 >> 1 का मान क्या होता है?)

(A) 4

(B) -4

(C) -5

(D) TypeError

Correct Answer: (C)

[EN] Arithmetic right shift equals floor division by 2**n: -9 // 2 = -5.

[HI] एरिथमेटिक राइट शिफ्ट 2**n से फ्लोर डिवीजन के बराबर होता है: -9 // 2 = -5।

Q6. Which error is raised by executing 5 << -1?

(5 << -1 चलाने पर कौन सा एरर उत्पन्न होता है?)

(A) TypeError

(B) ArithmeticError

(C) SyntaxError

(D) ValueError

Correct Answer: (D)

[EN] Negative shift counts are invalid for << and >> in Python and raise ValueError.

[HI] पायथन में << और >> के लिए ऋणात्मक शिफ्ट काउंट देने पर ValueError आता है।

Q7. Which operator was introduced in Python 3.8 as the walrus operator for inline assignment?

(पायथन 3.8 में इनलाइन असाइनमेंट के लिए 'वॉलरस ऑपरेटर' के रूप में किसे जोड़ा गया था?)

(A) :=

(B) <-

(C) ===

(D) =>

Correct Answer: (A)

[EN] PEP 572 introduced := (assignment expression) so variables can be bound inside expressions like if (n := len(s)) > 3:.

[HI] PEP 572 के अंतर्गत := (असाइनमेंट एक्सप्रेशन) जोड़ा गया ताकि एक्सप्रेशन के भीतर ही मान असाइन किया जा सके।

Q8. What happens when you run 5.0 & 3 in Python?

(पायथन में 5.0 & 3 चलाने पर क्या होता है?)

(A) Returns 1.0

(B) Raises TypeError

(C) Returns 1

(D) Returns 7

Correct Answer: (B)

[EN] Bitwise operators (&, |, ^, ~, <<, >>) only accept integer (or bool) operands, not floats.

[HI] बिटवाइज़ ऑपरेटर्स केवल int (या bool) पर काम करते हैं; float देने पर TypeError आता है।

Q9. How does a = [1, 2]; b = a; a += [3] affect b?

(a = [1, 2]; b = a; a += [3] चलाने पर b पर क्या प्रभाव पड़ता है?)

(A) Raises TypeError

(B) b also becomes [1, 2, 3] because += mutates lists in-place

(C) b remains [1, 2] because += creates a new list

(D) b becomes [3]

Correct Answer: (B)

[EN] For mutable lists, += invokes __iadd__ (equivalent to extend) in-place, so both aliases a and b see [1, 2, 3].

[HI] म्यूटेबल लिस्ट पर += इन-प्लेस परिवर्तन करता है, इसलिए a और b दोनों में [1, 2, 3] दिखाई देगा।

Q10. What is the output of: print(6 | 3 & 1)?

(print(6 | 3 & 1) का आउटपुट क्या होगा?)

(A) 3

(B) 1

(C) 7

(D) 6

Correct Answer: (C)

[EN] & has higher precedence than |, so 3 & 1 = 1 is evaluated first, then 6 | 1 = 7.

[HI] & की प्राथमिकता | से अधिक है, इसलिए पहले 3 & 1 = 1 और फिर 6 | 1 = 7 हल होगा।

Q11. What does ~(-1) return in Python?

(पायथन में ~(-1) क्या मान लौटाता है?)

(A) 1

(B) -1

(C) -2

(D) 0

Correct Answer: (D)

[EN] Using ~x == -(x + 1), ~(-1) = -(-1 + 1) = 0.

[HI] सूत्र ~x == -(x + 1) से ~(-1) = -(-1 + 1) = 0 होता है।

Q12. What is the result of 5 & 1 == 1 in Python?

(पायथन में 5 & 1 == 1 का परिणाम क्या होगा?)

(A) 1

(B) 0

(C) False

(D) True

Correct Answer: (D)

[EN] Unlike C, Python gives & higher precedence than ==, so it evaluates as (5 & 1) == 1 -> 1 == 1 -> True.

[HI] C के विपरीत पायथन में & की प्राथमिकता == से अधिक है, अतः यह (5 & 1) == 1 अर्थात True देता है।

Q13. Which bitwise expression reliably checks if an integer n is odd?

(कौन सा बिटवाइज़ व्यंजक किसी पूर्णांक n के विषम (odd) होने की सही जाँच करता है?)

(A) n & 1 == 1

(B) ~n == 1

(C) n ^ 1 == 1

(D) n | 1 == 1

Correct Answer: (A)

[EN] The least significant bit of any odd integer is 1, so n & 1 evaluates to 1 for odd numbers and 0 for even numbers.

[HI] हर विषम संख्या का अंतिम (LSB) बिट 1 होता है, इसलिए n & 1 का मान विषम के लिए 1 और सम के लिए 0 आता है।

Q14. What is the output of: x = 10; y = 20; x, y = y, x + y; print(x, y)?

(x = 10; y = 20; x, y = y, x + y; print(x, y) का आउटपुट क्या होगा?)

(A) 10 30

(B) 30 20

(C) 20 40

(D) 20 30

Correct Answer: (D)

[EN] Python evaluates the entire right-hand tuple (20, 10 + 20) = (20, 30) before unpacking into x and y.

[HI] पायथन अनपैक करने से पहले दाईं ओर के पूरे टपल (20, 10 + 20) = (20, 30) का मूल्यांकन करता है।

Q15. Among +, <<, &, and |, which has the HIGHEST precedence in Python?

(+, <<, & और | में से पायथन में किसकी प्राथमिकता सबसे अधिक है?)

(A) +

(B) <<

(C) |

(D) &

Correct Answer: (A)

[EN] Arithmetic addition (+) has higher precedence than bitwise shift (<<), which is higher than &, which is higher than |.

[HI] अंकगणितीय + की प्राथमिकता बिटवाइज़ शिफ्ट (<<) से अधिक होती है, उसके बाद & और फिर | आता है।

Q16. Which of the following statements about Python assignment and bitwise operators is NOT true?

(पायथन असाइनमेंट और बिटवाइज़ ऑपरेटर्स के बारे में कौन सा कथन सत्य नहीं है?)

(A) x ^= y; y ^= x; x ^= y swaps two integer variables without a temporary variable

(B) The '=' operator returns the assigned value and can be used directly inside an if condition

(C) Python bitwise shift left (<<) never loses high bits to fixed-width integer overflow

(D) Augmented assignment += rebinds a variable name when applied to an immutable tuple

Correct Answer: (B)

[EN] Simple assignment '=' is a statement, not an expression, so using it inside an if condition causes SyntaxError; use ':=' instead.

[HI] साधारण असाइनमेंट '=' एक स्टेटमेंट है (एक्सप्रेशन नहीं), इसलिए इसे सीधे if शर्त में लिखने पर SyntaxError आता है; इसके लिए ':=' का प्रयोग होता है।

Q17. Within Python Assignment and Bitwise Operators (&, |, ^, ~, <<, >>), which operator directly maps to: "Bitwise AND, OR, XOR"?

(Python Assignment and Bitwise Operators (&, |, ^, ~, <<, >>) में कौन सा Operator सीधे "Bitwise AND, OR, XOR" से संबंधित है?)

(A) ~

(B) & / | / ^

(C) << / >>

(D) += / :=

Correct Answer: (B)

[EN] In Python Assignment and Bitwise Operators (&, |, ^, ~, <<, >>), '& / | / ^' handles 'Bitwise AND, OR, XOR' (Higher precedence than == in Python).

[HI] Python Assignment and Bitwise Operators (&, |, ^, ~, <<, >>) में '& / | / ^' का कार्य/विवरण 'Bitwise AND, OR, XOR' (Higher precedence than == in Python) है।

Q18. Which option accurately describes '~' in Python Assignment and Bitwise Operators (&, |, ^, ~, <<, >>)?

(Python Assignment and Bitwise Operators (&, |, ^, ~, <<, >>) के संदर्भ में '~' की सही विशेषता कौन सी है?)

(A) Augmented & Walrus Assignment

(B) Bitwise AND, OR, XOR

(C) Bitwise NOT (1s/2s complement)

(D) Bitwise Left & Arithmetic Right Shift

Correct Answer: (C)

[EN] '~' provides 'Bitwise NOT (1s/2s complement)' — key point: Always evaluates to -(x + 1).

[HI] '~' का मुख्य विवरण 'Bitwise NOT (1s/2s complement)' है (Always evaluates to -(x + 1))।

Q19. In Python Assignment and Bitwise Operators (&, |, ^, ~, <<, >>), identify the correct parameter for the specification: "Bitwise Left & Arithmetic Right Shift".

(Python Assignment and Bitwise Operators (&, |, ^, ~, <<, >>) में "Bitwise Left & Arithmetic Right Shift" विवरण किस विकल्प के लिए सही है?)

(A) ~

(B) & / | / ^

(C) += / :=

(D) << / >>

Correct Answer: (D)

[EN] '<< / >>' corresponds to 'Bitwise Left & Arithmetic Right Shift' (x << n == x*(2**n); x >> n == x//(2**n)).

[HI] '<< / >>' का संबंध 'Bitwise Left & Arithmetic Right Shift' (x << n == x*(2**n); x >> n == x//(2**n)) से है।

Q20. Considering Python Assignment and Bitwise Operators (&, |, ^, ~, <<, >>), what key behavior defines '+= / :='?

(Python Assignment and Bitwise Operators (&, |, ^, ~, <<, >>) में '+= / :=' की मुख्य भूमिका या मान क्या है?)

(A) Bitwise Left & Arithmetic Right Shift

(B) Bitwise AND, OR, XOR

(C) Bitwise NOT (1s/2s complement)

(D) Augmented & Walrus Assignment

Correct Answer: (D)

[EN] '+= / :=' represents 'Augmented & Walrus Assignment' (+= mutates lists in-place; := is expr).

[HI] '+= / :=' का अर्थ/मान 'Augmented & Walrus Assignment' है (+= mutates lists in-place; := is expr)।

Python Assignment and Bitwise Operators (&, |, ^, ~, <<, >>) — Exam FAQs

Q1: Why is ~5 equal to -6 in Python instead of an unsigned 250?

पायथन में ~5 का मान 250 होने के बजाय -6 क्यों आता है?

[EN] Python integers have arbitrary precision with conceptual infinite sign-extension in two's complement, not fixed 8-bit unsigned width. Inverting all infinite leading zeros turns them into leading ones, which equals -(5 + 1) = -6.

[HI] पायथन के पूर्णांक 8-बिट अनसाइन्ड नहीं बल्कि असीमित चौड़ाई वाले टूज़ कॉम्प्लीमेंट पर आधारित होते हैं। आगे के सभी 0 बिट्स के 1 में बदलने से ~x हमेशा -(x + 1) अर्थात -6 बनता है।

Q2: Does a += b always behave identically to a = a + b?

क्या a += b हमेशा a = a + b की तरह ही व्यवहार करता है?

[EN] No. For immutable types (int, str, tuple) both create a new object and rebind a. For mutable types like list, a += b modifies the existing list in-place (preserving id(a)), whereas a = a + b creates a brand-new list.

[HI] नहीं। इम्यूटेबल (int, str, tuple) पर दोनों एक जैसा काम करते हैं, लेकिन list जैसे म्यूटेबल ऑब्जेक्ट पर a += b उसी पुरानी लिस्ट को बदलता है जबकि a = a + b एक नई लिस्ट बनाता है।

Q3: What is the difference between = and := in Python?

पायथन में = और := के बीच क्या अंतर है?

[EN] '=' is an assignment statement that cannot appear inside expressions like if/while conditions, whereas ':=' (walrus operator, Python 3.8+) is an assignment expression that binds and returns the value inline.

[HI] '=' एक स्टेटमेंट है जिसे if/while जैसी शर्तों के अंदर नहीं लिखा जा सकता, जबकि ':=' (वॉलरस ऑपरेटर) एक एक्सप्रेशन है जो मान असाइन करने के साथ उसे वहीं लौटाता भी है।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)