Bitwise Operators in C: &, |, ^, ~, <<, >> Operations Notes in Hindi & English : बिटवाइज़ ऑपरेटर्स: AND, OR, XOR, NOT और बिट शिफ्टिंग नियम और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

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

Overview: Bitwise operators in C (&, |, ^, ~, <<, >>) manipulate individual bits of integer types for low-level hardware control, bitmasks, flags, and fast power-of-two scaling. Avoiding precedence traps against == and Undefined Behavior during negative or oversized shifts is vital in C exams.

अध्याय सार: C में बिटवाइज़ ऑपरेटर्स (&, |, ^, ~, <<, >>) हार्डवेयर नियंत्रण, बिटमास्क, फ्लैग्स और तेज गणना के लिए पूर्णांकों के व्यक्तिगत बिट्स पर कार्य करते हैं। परीक्षा की दृष्टि से == के साथ प्राथमिकता भ्रम और गलत शिफ्टिंग से होने वाले Undefined Behavior से बचना अत्यंत महत्वपूर्ण है।

Bitwise Operators in C: &, |, ^, ~, <<, >> Operations: Detailed Notes & Architecture (बिटवाइज़ ऑपरेटर्स: AND, OR, XOR, NOT और बिट शिफ्टिंग नियम)

1. Bitwise &, |, ^ and Bitmask Manipulation Idioms (बिटवाइज़ &, |, ^ और बिटमास्क नियम)

[EN] Bitwise AND (&), OR (|), and XOR (^) operate pairwise across each bit position of integer operands (floats and doubles are disallowed). AND outputs 1 only when both bits are 1; OR outputs 1 when either bit is 1; XOR outputs 1 when bits differ. Embedded and system programmers rely on four universal bitmask idioms for bit index k (0-based): set bit (`x |= (1U << k)`), clear bit (`x &= ~(1U << k)`), toggle bit (`x ^= (1U << k)`), and test bit (`(x >> k) & 1U`). Furthermore, `(n > 0 && (n & (n - 1)) == 0)` tests if n is an exact power of two in O(1) time.

[HI] बिटवाइज़ AND (&), OR (|) और XOR (^) पूर्णांक ऑपरेंड के प्रत्येक संगत बिट पर अलग-अलग कार्य करते हैं (float/double पर ये अवैध हैं)। बिट इंडेक्स k को नियंत्रित करने के चार प्रमुख सूत्र हैं: बिट सेट करना (`x |= (1U << k)`), क्लियर करना (`x &= ~(1U << k)`), टॉगल/पलटना (`x ^= (1U << k)`) और जांचना (`(x >> k) & 1U`)। इसके अलावा `n & (n - 1)` सबसे दाएँ सेट बिट को हटाता है, जिससे `(n & (n - 1)) == 0` द्वारा 2 की घात (Power of 2) जांची जाती है।

2. Bitwise Complement (~) & Two's Complement Arithmetic (बिटवाइज़ कॉम्प्लिमेंट (~) व टूज़ कॉम्प्लिमेंट नियम)

[EN] The unary bitwise NOT operator (~) flips every bit (0 becomes 1 and 1 becomes 0), including the sign bit on signed integers. On standard two's complement systems, negating a number satisfies `-x == ~x + 1`, which implies the identity `~x == -(x + 1)`. For example, in 32-bit signed int, `~5` equals `-6`, `~(-1)` equals `0`, and `~0` yields `-1` (all bits set to 1). Because `char` or `short` undergoes integer promotion to `int` before `~` applies, `unsigned char c = 0x00; ~c;` evaluates to `int -1` (0xFFFFFFFF), NOT unsigned 255.

[HI] यूनरी बिटवाइज़ NOT (`~`) साइन बिट सहित हर एक बिट को उलट देता है। मानक टूज़ कॉम्प्लिमेंट सिस्टम में `~x` का मान हमेशा `-(x + 1)` के बराबर होता है—जैसे 32-बिट साइन्ड int में `~5` का मान `-6` और `~0` का मान `-1` होता है। ध्यान रहे कि छोटे टाइप (जैसे `unsigned char`) पर `~` लगने से पहले इंटीजर प्रमोशन होकर वह `int` में बदल जाता है।

3. Left Shift (<<) and Right Shift (>>): Standard C Edge Cases (लेफ्ट शिफ्ट (<<) और राइट शिफ्ट (>>): मानक C के नियम)

[EN] Left shift (`x << n`) moves bits left by n positions and fills vacated LSB bits with 0s, multiplying unsigned values by 2^n modulo 2^width. Right shift (`x >> n`) on unsigned types always performs a logical shift (filling MSB with 0s), dividing by 2^n. However, right-shifting a negative signed integer is implementation-defined in C (usually arithmetic shift preserving sign bit, but not guaranteed portable). Crucially, ISO C declares Undefined Behavior if: (1) shift count n is negative, (2) n is greater than or equal to the promoted bit-width (e.g., `1 << 32` on 32-bit int), or (3) left-shifting a negative signed integer (`-1 << 2`).

[HI] लेफ्ट शिफ्ट (`x << n`) बिट्स को n स्थान बाएँ खिसकाकर दाईं ओर 0 भरता है। अनसाइन्ड मानों पर राइट शिफ्ट (`x >> n`) हमेशा लॉजिकल शिफ्ट (बाईं ओर 0 भरना) होता है जो 2^n से भाग देने जैसा है, किंतु ऋणात्मक साइन्ड संख्या पर राइट शिफ्ट करना C में कंपाइलर-निर्भर (प्रायः अरिथमेटिक शिफ्ट) होता है। यदि शिफ्ट काउंट ऋणात्मक हो, डेटा टाइप की कुल बिट चौड़ाई के बराबर या उससे अधिक हो (जैसे 32-बिट int पर `1 << 32`), या ऋणात्मक संख्या को लेफ्ट शिफ्ट किया जाए, तो C में Undefined Behavior होता है।

4. XOR Properties, Swap Aliasing Trap & Precedence Bug (XOR गुण, स्वैप ट्रैप और प्राथमिकता त्रुटि)

[EN] XOR possesses four algebraic properties: `a ^ a == 0`, `a ^ 0 == a`, commutativity, and associativity—allowing fast single-number discovery in paired arrays. While the XOR swap (`a ^= b; b ^= a; a ^= b;`) exchanges two distinct variables without a temporary variable, it catastrophically zeroes out the value if `a` and `b` alias the same memory location (e.g., `*p` and `*q` pointing to the same int)! Another notorious bug is operator precedence: `==` and `!=` bind TIGHTER than `&`, `^`, and `|`. Writing `if (flags & MASK == MASK)` parses as `flags & (MASK == MASK)` (`flags & 1`), so parentheses `(flags & MASK) == MASK` are mandatory.

[HI] XOR में `a ^ a = 0` तथा `a ^ 0 = a` जैसे विशेष गुण होते हैं। बिना तीसरे वेरिएबल के XOR स्वैप (`a ^= b; b ^= a; a ^= b;`) तब विफल होकर मान को शून्य कर देता है जब दोनों पॉइंटर एक ही मेमोरी पते (Aliasing) को इंगित कर रहे हों। एक अन्य बड़ी परीक्षा त्रुटि प्राथमिकता से जुड़ी है: `==` और `!=` की प्राथमिकता `&`, `^`, `|` से अधिक होती है, इसलिए `(flags & MASK) == MASK` में कोष्ठक लगाना अनिवार्य है।

Syntax, Formula & Technical Example (Bitwise Operators in C: &, |, ^, ~, <<, >> Operations)

#include <stdio.h>
int main() {
    unsigned int x = 12, y = 10; // 1100 & 1010
    printf("AND=%u OR=%u XOR=%u ", x & y, x | y, x ^ y);
    printf("SH=%u POW2=%d\n", 5U << 2, (8 & 7) == 0);
    return 0;
}
// Output: AND=8 OR=14 XOR=6 SH=20 POW2=1

Bitwise Operators in C: &, |, ^, ~, <<, >> Operations Quick Reference & Comparison Table

Bitwise OperatorBit-Level OperationStandard C / Idiom Rule
Bitwise AND (&)Outputs 1 only when both bits are 1Used to clear or test bits via mask
Bitwise OR (|)Outputs 1 when either bit is 1Used to turn on/set flag bits
Bitwise XOR (^)Outputs 1 only when corresponding bits differUsed to toggle bits; x ^ x equals 0
Bitwise NOT (~)Inverts every bit; ~x equals -(x + 1)Unary operator subject to integer promotion

Bitwise Operators in C: &, |, ^, ~, <<, >> Operations Top 20 Bilingual MCQs with Answers

Q1. What are the values of `12 & 10`, `12 | 10`, and `12 ^ 10` respectively?

(`12 & 10`, `12 | 10` और `12 ^ 10` के मान क्रमशः क्या होंगे?)

(A) 6, 14, 8

(B) 8, 14, 6

(C) 8, 12, 6

(D) 10, 14, 4

Correct Answer: (B)

[EN] In binary, 12 is 1100 and 10 is 1010: AND = 1000 (8), OR = 1110 (14), XOR = 0110 (6).

[HI] बाइनरी में 12 = 1100 और 10 = 1010 है: AND = 1000 (8), OR = 1110 (14) तथा XOR = 0110 (6) प्राप्त होता है।

Q2. On a 32-bit two's complement system, what does `printf("%d", ~5);` output?

(32-बिट टूज़ कॉम्प्लिमेंट सिस्टम पर `printf("%d", ~5);` क्या आउटपुट देगा?)

(A) -5

(B) 6

(C) 4294967290

(D) -6

Correct Answer: (D)

[EN] Two's complement identity gives ~x = -(x + 1), so ~5 = -(5 + 1) = -6.

[HI] टूज़ कॉम्प्लिमेंट नियमानुसार ~x = -(x + 1) होता है, इसलिए ~5 = -6 प्रिंट होगा।

Q3. Which bitwise expression toggles (flips) the `k`-th bit (0-indexed) of an unsigned integer `x`?

(अनसाइन्ड पूर्णांक `x` के `k`-वें बिट (0-आधारित) को टॉगल (उलटने) के लिए कौन सा व्यंजक सही है?)

(A) x &= ~(1U << k)

(B) x &= (1U << k)

(C) x ^= (1U << k)

(D) x |= (1U << k)

Correct Answer: (C)

[EN] XOR with 1 flips the target bit (0->1, 1->0) while XOR with 0 leaves all other bits unchanged.

[HI] 1 के साथ XOR करने पर संबंधित बिट पलट जाता है जबकि 0 वाले अन्य सभी बिट अपरिवर्तित रहते हैं।

Q4. Which expression tests whether a positive integer `n > 0` is an exact power of 2?

(धनात्मक पूर्णांक `n > 0` 2 की सटीक घात (Power of 2) है या नहीं, यह जांचने का सही व्यंजक कौन सा है?)

(A) (n | (n - 1)) == 0

(B) (n ^ (n - 1)) == 0

(C) (n & (n - 1)) == 0

(D) (n & 1) == 0

Correct Answer: (C)

[EN] Powers of two have exactly one 1-bit; subtracting 1 flips that bit and all lower bits, making n & (n - 1) zero.

[HI] 2 की घात वाली संख्या में केवल एक ही 1-बिट होता है; n & (n - 1) उस सबसे दाएँ 1-बिट को शून्य कर देता है।

Q5. What does `printf("%d", 6 & 2 == 2);` output in C?

(C में `printf("%d", 6 & 2 == 2);` क्या आउटपुट देता है?)

(A) 0

(B) 6

(C) 1

(D) 2

Correct Answer: (A)

[EN] Equality (==) has HIGHER precedence than bitwise AND (&)! Thus 6 & (2 == 2) = 6 & 1 = 0110 & 0001 = 0.

[HI] == की प्राथमिकता & से अधिक होती है! अतः पहले 2 == 2 से 1 बनता है और फिर 6 & 1 (0110 & 0001) = 0 आता है।

Q6. In ISO C, what happens when you shift a 32-bit `unsigned int` left by 32 bits (`x << 32`)?

(ISO C में 32-बिट `unsigned int` को 32 बिट्स बाएँ शिफ्ट (`x << 32`) करने पर क्या होता है?)

(A) Wraps around to x << 0

(B) Compilation error by ISO standard

(C) Undefined Behavior

(D) Always evaluates cleanly to 0

Correct Answer: (C)

[EN] ISO C strictly states that shifting by a negative count or by >= the bit-width of the promoted LHS is Undefined Behavior.

[HI] ISO C मानक के अनुसार ऑपरेंड की बिट-चौड़ाई के बराबर या उससे अधिक (या ऋणात्मक) संख्या से शिफ्ट करना Undefined Behavior है।

Q7. How does `x >> 2` behave on a standard `unsigned int x = 40;`?

(सामान्य `unsigned int x = 40;` पर `x >> 2` किस प्रकार कार्य करता है?)

(A) Multiplies 40 by 4 giving 160

(B) Raises Undefined Behavior

(C) Performs arithmetic shift filling 1s from the left

(D) Performs a logical right shift filling 0s from the left, yielding 10

Correct Answer: (D)

[EN] Unsigned right shift is always logical (0-fill), equivalent to floor division by 2^2 = 4 (40 / 4 = 10).

[HI] अनसाइन्ड राइट शिफ्ट सदैव लॉजिकल (बाईं ओर 0 भरना) होता है, जो 40 / 4 = 10 देता है।

Q8. Why can the no-temp XOR swap (`*a ^= *b; *b ^= *a; *a ^= *b;`) fail catastrophically?

(बिना अतिरिक्त वेरिएबल वाला XOR स्वैप (`*a ^= *b; *b ^= *a; *a ^= *b;`) कब विफल हो सकता है?)

(A) When both numbers are odd

(B) When numbers exceed 255

(C) When pointers a and b point to the exact same memory address (aliasing), zeroing the value

(D) When either number is negative

Correct Answer: (C)

[EN] If a == b, the first step *a ^= *a sets that single memory location to 0, destroying the stored data.

[HI] यदि a और b एक ही वेरिएबल का पता रखते हों, तो पहला ही चरण `*a ^= *a` उस मान को हमेशा के लिए 0 कर देता है।

Q9. What does `(0x5A & 0x0F)` extract in hexadecimal?

(हेक्साडेसिमल में `(0x5A & 0x0F)` क्या मान निकालता है?)

(A) 0x5F

(B) 0x0A (the lower 4 bits / nibble)

(C) 0x05

(D) 0x50 (the upper nibble)

Correct Answer: (B)

[EN] 0x0F (0000 1111) acts as a bitmask preserving only the lower nibble of 0x5A (0101 1010 -> 0000 1010 = 0x0A).

[HI] 0x0F (0000 1111) ऊपरी 4 बिट्स को शून्य कर देता है और निचले 4 बिट्स (0x0A) को सुरक्षित निकालता है।

Q10. What is the value of `5 ^ 7 ^ 5 ^ 9 ^ 7` in C?

(C में `5 ^ 7 ^ 5 ^ 9 ^ 7` का मान क्या होगा?)

(A) 0

(B) 5

(C) 7

(D) 9

Correct Answer: (D)

[EN] XOR is associative and commutative; pairs cancel out (5^5=0, 7^7=0), leaving 0 ^ 9 = 9.

[HI] XOR क्रम-विनिमेय व साहचर्य नियम का पालन करता है, अतः जोड़े (5^5 और 7^7) शून्य होकर अकेला 9 बचता है।

Q11. Which bitwise expression clears bit 3 (0-indexed) of `unsigned int reg` without changing other bits?

(`unsigned int reg` के अन्य बिट्स बदले बिना बिट 3 (0-आधारित) को क्लियर (0) करने का सही कोड क्या है?)

(A) reg &= (1U << 3)

(B) reg |= ~(1U << 3)

(C) reg ^= (1U << 3)

(D) reg &= ~(1U << 3)

Correct Answer: (D)

[EN] ~(1U << 3) creates a mask with 0 at bit 3 and 1s everywhere else; ANDing clears bit 3.

[HI] ~(1U << 3) बिट 3 पर 0 और बाकी सभी स्थानों पर 1 वाला मास्क बनाता है, जिसे &= करने से बिट 3 शून्य हो जाता है।

Q12. According to C precedence rules, order the bitwise operators `&`, `^`, `|` from HIGHEST to LOWEST:

(C के प्राथमिकता नियम अनुसार `&`, `^`, `|` का उच्चतम से निम्नतम क्रम क्या है?)

(A) & > ^ > |

(B) ^ > & > |

(C) | > ^ > &

(D) All three share equal precedence

Correct Answer: (A)

[EN] Bitwise AND (&) is tier 8, XOR (^) is tier 9, and OR (|) is tier 10 (still above logical && and ||).

[HI] बिटवाइज़ AND (&) की प्राथमिकता सबसे अधिक, फिर XOR (^) और उसके बाद OR (|) की प्राथमिकता आती है।

Q13. In C, right-shifting a negative signed integer (e.g., `-8 >> 1`) is classified by the ISO C standard as:

(ISO C मानक के अनुसार ऋणात्मक साइन्ड पूर्णांक पर राइट शिफ्ट (जैसे `-8 >> 1`) किस श्रेणी में आता है?)

(A) Implementation-defined behavior

(B) Strictly guaranteed arithmetic shift across all compilers

(C) Compile-time syntax error

(D) Strictly guaranteed logical shift across all compilers

Correct Answer: (A)

[EN] ISO C leaves signed right shift of negative values implementation-defined (most CPUs do arithmetic shift, giving -4, though not strictly mandated by C).

[HI] ISO C में ऋणात्मक मान का राइट शिफ्ट कंपाइलर-निर्भर (Implementation-defined) होता है, यद्यपि अधिकतर आधुनिक सिस्टम अरिथमेटिक शिफ्ट करते हैं।

Q14. Given `unsigned char a = 0;`, what does `printf("%d", ~a);` print on a 32-bit `int` system?

(32-बिट `int` वाले सिस्टम पर `unsigned char a = 0;` के लिए `printf("%d", ~a);` क्या प्रिंट करेगा?)

(A) -1

(B) 255

(C) 1

(D) 0

Correct Answer: (A)

[EN] Before ~ executes, integer promotion widens unsigned char 0 to signed int 0; ~0 in signed int is -1.

[HI] ~ लागू होने से पहले इंटीजर प्रमोशन के कारण unsigned char 0, int 0 में बदलता है और ~0 का मान -1 प्रिंट होता है।

Q15. Which statement is NOT true about bitwise operators in C?

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

(A) Bitwise ~ has higher precedence than << and >>

(B) Bitwise & has lower precedence than == and !=

(C) Left-shifting a negative signed integer invokes Undefined Behavior in C

(D) Bitwise operators can be applied directly to float and double operands

Correct Answer: (D)

[EN] Bitwise operators strictly require integer operands; applying them to float or double causes a compile error.

[HI] बिटवाइज़ ऑपरेटर्स केवल पूर्णांकों पर कार्य करते हैं; float या double पर इन्हें लगाने से कंपाइल त्रुटि आती है।

Q16. How can `(x * 8) + x` (i.e. multiplying integer `x >= 0` by 9) be written efficiently using bit shifts?

(धनात्मक पूर्णांक `x` को 9 से गुणा करने के लिए बिट शिफ्ट का सही और कुशल रूप कौन सा है?)

(A) x << 3 + x

(B) (x << 9)

(C) (x << 3) + x

(D) (x >> 3) + x

Correct Answer: (C)

[EN] Parentheses are critical because + binds tighter than <<: x << 3 computes 8*x, plus x gives 9*x (whereas x << 3 + x shifts by 3+x!).

[HI] + की प्राथमिकता << से अधिक होने के कारण कोष्ठक अनिवार्य है: (x << 3) + x से 8x + x = 9x मिलता है।

Q17. Within Bitwise Operators in C: &, |, ^, ~, <<, >> Operations, which bitwise operator directly maps to: "Outputs 1 only when both bits are 1"?

(Bitwise Operators in C: &, |, ^, ~, <<, >> Operations में कौन सा Bitwise Operator सीधे "Outputs 1 only when both bits are 1" से संबंधित है?)

(A) Bitwise OR (|)

(B) Bitwise AND (&)

(C) Bitwise XOR (^)

(D) Bitwise NOT (~)

Correct Answer: (B)

[EN] In Bitwise Operators in C: &, |, ^, ~, <<, >> Operations, 'Bitwise AND (&)' handles 'Outputs 1 only when both bits are 1' (Used to clear or test bits via mask).

[HI] Bitwise Operators in C: &, |, ^, ~, <<, >> Operations में 'Bitwise AND (&)' का कार्य/विवरण 'Outputs 1 only when both bits are 1' (Used to clear or test bits via mask) है।

Q18. Which option accurately describes 'Bitwise OR (|)' in Bitwise Operators in C: &, |, ^, ~, <<, >> Operations?

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

(A) Inverts every bit; ~x equals -(x + 1)

(B) Outputs 1 only when both bits are 1

(C) Outputs 1 when either bit is 1

(D) Outputs 1 only when corresponding bits differ

Correct Answer: (C)

[EN] 'Bitwise OR (|)' provides 'Outputs 1 when either bit is 1' — key point: Used to turn on/set flag bits.

[HI] 'Bitwise OR (|)' का मुख्य विवरण 'Outputs 1 when either bit is 1' है (Used to turn on/set flag bits)।

Q19. In Bitwise Operators in C: &, |, ^, ~, <<, >> Operations, identify the correct parameter for the specification: "Outputs 1 only when corresponding bits differ".

(Bitwise Operators in C: &, |, ^, ~, <<, >> Operations में "Outputs 1 only when corresponding bits differ" विवरण किस विकल्प के लिए सही है?)

(A) Bitwise NOT (~)

(B) Bitwise XOR (^)

(C) Bitwise AND (&)

(D) Bitwise OR (|)

Correct Answer: (B)

[EN] 'Bitwise XOR (^)' corresponds to 'Outputs 1 only when corresponding bits differ' (Used to toggle bits; x ^ x equals 0).

[HI] 'Bitwise XOR (^)' का संबंध 'Outputs 1 only when corresponding bits differ' (Used to toggle bits; x ^ x equals 0) से है।

Q20. Considering Bitwise Operators in C: &, |, ^, ~, <<, >> Operations, what key behavior defines 'Bitwise NOT (~)'?

(Bitwise Operators in C: &, |, ^, ~, <<, >> Operations में 'Bitwise NOT (~)' की मुख्य भूमिका या मान क्या है?)

(A) Outputs 1 only when corresponding bits differ

(B) Outputs 1 when either bit is 1

(C) Inverts every bit; ~x equals -(x + 1)

(D) Outputs 1 only when both bits are 1

Correct Answer: (C)

[EN] 'Bitwise NOT (~)' represents 'Inverts every bit; ~x equals -(x + 1)' (Unary operator subject to integer promotion).

[HI] 'Bitwise NOT (~)' का अर्थ/मान 'Inverts every bit; ~x equals -(x + 1)' है (Unary operator subject to integer promotion)।

Bitwise Operators in C: &, |, ^, ~, <<, >> Operations — Exam FAQs

Q1: Why does `(flags & MASK == MASK)` behave unexpectedly in C?

C में `(flags & MASK == MASK)` बिना अंदरूनी कोष्ठक के गलत उत्तर क्यों देता है?

[EN] Equality (`==`) has higher precedence than bitwise AND (`&`). Without parentheses around `flags & MASK`, the compiler parses it as `flags & (MASK == MASK)`, which equals `flags & 1`. Always write `(flags & MASK) == MASK`.

[HI] `==` की प्राथमिकता `&` से अधिक होने के कारण कंपाइलर इसे `flags & (MASK == MASK)` यानी `flags & 1` मान लेता है। इसलिए हमेशा `(flags & MASK) == MASK` लिखें।

Q2: Why is shifting a 32-bit signed integer by 31 (`1 << 31`) dangerous in C?

C में 32-बिट साइन्ड int पर `1 << 31` लिखना खतरनाक क्यों माना जाता है?

[EN] In C99/C11, shifting a positive signed `int` into or past the sign bit (`1 << 31` on 32-bit int) triggers Undefined Behavior. Always use an unsigned literal like `1U << 31` when building MSB bitmasks.

[HI] C मानकों में साइन्ड धनात्मक मान को साइन बिट में शिफ्ट करना (`1 << 31`) Undefined Behavior है। सुरक्षित बिटमास्क के लिए हमेशा अनसाइन्ड लिटरल `1U << 31` उपयोग करें।

Q3: How does `n & (n - 1)` work to clear the lowest set bit?

`n & (n - 1)` किसी संख्या के सबसे दाएँ 1-बिट को कैसे हटाता है?

[EN] Subtracting 1 flips the rightmost `1` bit to `0` and turns all trailing `0` bits to `1`s while leaving higher bits untouched. Performing bitwise `&` with `n` clears that lowest `1` bit and all lower bits in a single CPU instruction.

[HI] n में से 1 घटाने पर सबसे दायाँ 1 बिट 0 हो जाता है और उसके दाएँ के सभी 0 बिट 1 बन जाते हैं; फिर n के साथ & करने पर वह सबसे निचला 1 बिट स्वतः शून्य हो जाता है।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)