PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Shift Operators in Java: << (Left), >> (Signed Right), >>> (Unsigned Right) (शिफ्ट ऑपरेटर्स: लेफ्ट शिफ्ट, साइन्ड राइट और अनसाइन्ड राइट शिफ्ट) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Java provides three bitwise shift operators: left shift (<<), signed arithmetic right shift (>>), and unsigned logical right shift (>>>). Because Java has no unsigned left shift (<<<) and applies strict 5-bit/6-bit distance masking alongside int promotion for byte, short, and char, understanding sign-bit fill behavior is critical for low-level code and exams.
अध्याय सार: जावा में तीन बिटवाइज़ शिफ्ट ऑपरेटर उपलब्ध हैं: लेफ्ट शिफ्ट (<<), साइन्ड अरिथमेटिक राइट शिफ्ट (>>) और अनसाइन्ड लॉजिकल राइट शिफ्ट (>>>)। जावा में अनसाइन्ड लेफ्ट शिफ्ट (<<<) नहीं होता तथा byte, short और char के int प्रमोशन एवं 5-बिट/6-बिट शिफ्ट मास्किंग के नियम लागू होते हैं, जिन्हें समझना परीक्षाओं के लिए अत्यंत महत्वपूर्ण है।
Shift Operators in Java: << (Left), >> (Signed Right), >>> (Unsigned Right): Detailed Notes & Architecture (शिफ्ट ऑपरेटर्स: लेफ्ट शिफ्ट, साइन्ड राइट और अनसाइन्ड राइट शिफ्ट)
1. Left Shift Operator (<<) Mechanics and Multiplication Equivalence (लेफ्ट शिफ्ट ऑपरेटर (<<) कार्यप्रणाली और गुणन तुल्यता)
[EN] The left shift operator (x << n) moves every bit of x to the left by n positions, discarding bits pushed past the Most Significant Bit (MSB) and filling all vacated rightmost positions with zeros (0). Within the value range where no significant bits overflow into or past the sign bit, shifting left by n is equivalent to multiplying by 2^n (for example, 5 << 3 == 5 * 8 == 40). If a 1-bit shifts into the 32nd MSB position of an int, the number flips negative under two's-complement rules (for example, 1 << 31 == Integer.MIN_VALUE == -2147483648). Java does NOT define a '<<<' operator because left-shifting always inserts 0s on the right regardless of sign.
[HI] लेफ्ट शिफ्ट ऑपरेटर (x << n) x के सभी बिट्स को n स्थान बाईं ओर खिसकाता है, MSB से बाहर निकलने वाले बिट्स को त्याग देता है और दाईं ओर खाली हुए सभी स्थानों पर 0 भरता है। जब तक साइन बिट में ओवरफ़्लो न हो, n से लेफ्ट शिफ्ट करना 2^n से गुणा करने के बराबर होता है (जैसे 5 << 3 == 40)। यदि 1-बिट खिसककर 32-बिट int के सबसे बाएँ MSB स्थान पर आ जाए, तो संख्या टूज़-कॉम्प्लिमेंट के कारण ऋणात्मक हो जाती है (जैसे 1 << 31 == -2147483648)। जावा में '<<<' नाम का कोई ऑपरेटर नहीं है क्योंकि लेफ्ट शिफ्ट में दाईं ओर हमेशा 0 ही भरा जाता है।
2. Signed Right Shift (>>) and Sign-Extension Behavior (साइन्ड राइट शिफ्ट (>>) और साइन-एक्सटेंशन नियम)
[EN] The signed (arithmetic) right shift operator (x >> n) shifts all bits of x to the right by n positions and copies the original Most Significant Bit (sign bit) into every vacated leftmost position. For non-negative numbers (MSB = 0), vacated left bits receive 0; for negative numbers (MSB = 1), vacated left bits receive 1, preserving the negative sign. Unlike integer division (/ 2^n) which truncates toward zero, signed right shift divides by 2^n with floor rounding toward negative infinity: -5 / 2 == -2, whereas -5 >> 1 == -3! Repeatedly shifting -1 right (-1 >> n) always remains -1 because all 32 bits are 1 and every vacated MSB is refilled with 1.
[HI] साइन्ड (अरिथमेटिक) राइट शिफ्ट ऑपरेटर (x >> n) बिट्स को n स्थान दाईं ओर खिसकाता है और बाईं ओर खाली हुए हर स्थान पर मूल साइन बिट (MSB) की प्रतिलिपि भरता है। धनात्मक संख्याओं (MSB = 0) के लिए बाईं ओर 0 भरता है, जबकि ऋणात्मक संख्याओं (MSB = 1) के लिए बाईं ओर 1 भरकर ऋणात्मक चिह्न सुरक्षित रखा जाता है। पूर्णांक विभाजन (/ 2) के विपरीत जो शून्य की ओर काटता है, >> नकारात्मक अनंत की ओर फ्लोर राउंडिंग करता है: -5 / 2 == -2 होता है, जबकि -5 >> 1 == -3 होता है। -1 को कितनी भी बार >> से शिफ्ट करने पर (-1 >> n) परिणाम हमेशा -1 ही रहता है क्योंकि सभी बिट्स 1 रहते हैं।
3. Unsigned Right Shift (>>>) for Bit Manipulation and Hashing (बिट हेरफेर और हैशिंग के लिए अनसाइन्ड राइट शिफ्ट (>>>))
[EN] The unsigned (logical) right shift operator (x >>> n) shifts all bits right by n positions and unconditionally inserts 0 into every vacated leftmost position regardless of whether x was positive or negative. For positive numbers, >> and >>> produce identical results; for negative numbers, >>> immediately flips the sign bit to 0, transforming a negative value into a large positive integer. For example, -1 (0xFFFFFFFF) >>> 1 yields 0x7FFFFFFF (2,147,483,647 = Integer.MAX_VALUE), and -1 >>> 24 extracts the top 8 bits as unsigned 255. Java standard libraries heavily rely on >>> in algorithms like binary search midpoint calculation 'int mid = (low + high) >>> 1;' to prevent signed overflow bugs and in HashMap bit spreading.
[HI] अनसाइन्ड (लॉजिकल) राइट शिफ्ट ऑपरेटर (x >>> n) बिट्स को दाईं ओर खिसकाकर संख्या के चिह्न की परवाह किए बिना बाईं ओर हमेशा 0 भरता है। धनात्मक संख्याओं पर >> और >>> समान परिणाम देते हैं, परंतु ऋणात्मक संख्याओं पर >>> तुरंत साइन बिट को 0 करके उसे एक बड़े धनात्मक पूर्णांक में बदल देता है। उदाहरण के लिए, -1 (0xFFFFFFFF) >>> 1 का मान 0x7FFFFFFF (2,147,483,647) होता है। बाइनरी सर्च में ओवरफ़्लो से बचने हेतु 'int mid = (low + high) >>> 1;' तथा HashMap हैशिंग में >>> का व्यापक उपयोग होता है।
4. Int Promotion and Shift-Distance Masking Rules (JLS §15.19) (Int प्रमोशन और शिफ्ट-दूरी मास्किंग नियम (JLS §15.19))
[EN] Before executing any shift operator, Java promotes left operands of type byte, short, or char to 32-bit int—so shifting 'byte b = -1; b >>> 1' shifts 32-bit 0xFFFFFFFF into 0x7FFFFFFF (2147483647), not 8-bit 127! Crucially, Java masks the right-hand shift distance before shifting: if the left operand is int (32-bit), only the lowest 5 bits of the shift count are used (n & 31, i.e., n % 32 for non-negative n). Consequently, '1 << 32' shifts by 32 & 31 = 0 bits and evaluates to 1, while '1 << 33' evaluates to 2 (1 << 1)! Similarly, when the left operand is long (64-bit), only the lowest 6 bits of the shift distance are used (n & 63), so '1L << 64' evaluates to 1L.
[HI] किसी भी शिफ्ट ऑपरेटर को चलाने से पहले जावा byte, short या char के बाएँ ऑपरेंड को 32-बिट int में प्रमोट करता है—अतः 'byte b = -1; b >>> 1' पहले 32-बिट 0xFFFFFFFF बनकर 2147483647 देता है। शिफ्ट से पहले जावा दाएँ पक्ष की शिफ्ट दूरी को मास्क करता है: यदि बायाँ ऑपरेंड int (32-बिट) है, तो दूरी के केवल निचले 5 बिट्स (n & 31) उपयोग होते हैं। इसी कारण '1 << 32' वास्तव में 0 बिट्स शिफ्ट करके 1 देता है, और '1 << 33' वास्तव में 1 << 1 अर्थात 2 देता है। इसी तरह long (64-बिट) बाएँ ऑपरेंड के लिए केवल निचले 6 बिट्स (n & 63) उपयोग होते हैं, जिससे '1L << 64' का मान 1L आता है।
Syntax, Formula & Technical Example (Shift Operators in Java: << (Left), >> (Signed Right), >>> (Unsigned Right))
public class ShiftOperatorsDemo {
public static void main(String[] args) {
int pos = 8, neg = -8;
System.out.println("8 << 2 = " + (pos << 2)); // 32 (8 * 4)
System.out.println("-8 >> 1 = " + (neg >> 1)); // -4 (sign bit 1 kept)
System.out.println("-1 >>> 1 = " + (-1 >>> 1)); // 2147483647 (0 filled at MSB)
System.out.println("-5 >> 1 = " + (-5 >> 1)); // -3 (floor toward -infinity)
System.out.println("1 << 32 = " + (1 << 32)); // 1 (32 & 31 == 0 shift)
}
}
Shift Operators in Java: << (Left), >> (Signed Right), >>> (Unsigned Right) Quick Reference & Comparison Table
| Shift Operator / Rule | Bit Fill & Behavior | Key Exam Fact |
|---|---|---|
| << (Left Shift) | Shifts left; fills trailing right bits with 0 | Multiplies by 2^n without overflow; no <<< exists in Java |
| >> (Signed Right) | Shifts right; preserves sign bit (MSB) on left | Maintains negative sign; -5 >> 1 evaluates to -3 |
| >>> (Unsigned Right) | Shifts right; always fills leading left bits with 0 | Turns negative int positive; -1 >>> 1 == Integer.MAX_VALUE |
| Shift Distance Mask | Uses lower 5 bits (int) or 6 bits (long) of count | 1 << 32 equals 1; 1L << 64 equals 1L |
Shift Operators in Java: << (Left), >> (Signed Right), >>> (Unsigned Right) Top 20 Bilingual MCQs with Answers
Q1. What is the exact output of: System.out.println(-1 >>> 1);
(System.out.println(-1 >>> 1); का सटीक आउटपुट क्या होगा?)
(A) 2147483647
(B) -1
(C) -2147483648
(D) 0
Correct Answer: (A)
[EN] -1 in 32-bit two's complement is 0xFFFFFFFF (all 32 bits are 1). Unsigned right shift >>> inserts a 0 at the MSB, producing 0x7FFFFFFF = Integer.MAX_VALUE (2147483647).
[HI] 32-बिट टूज़-कॉम्प्लिमेंट में -1 का मान 0xFFFFFFFF (सभी 32 बिट्स 1) होता है। >>> से सबसे बाएँ 0 भरने पर 0x7FFFFFFF अर्थात 2147483647 मिलता है।
Q2. What is the output of: System.out.println(-5 >> 1);
(System.out.println(-5 >> 1); का आउटपुट क्या होगा?)
(A) -3
(B) 2147483645
(C) -2
(D) 0
Correct Answer: (A)
[EN] -5 in 32-bit binary ends in ...11111011. Shifting right by 1 with sign extension produces ...11111101, which equals -3 (floor division by 2).
[HI] -5 के अंत में ...11111011 होता है; साइन-एक्सटेंशन के साथ 1 बिट दाएँ शिफ्ट करने पर ...11111101 अर्थात -3 मिलता है।
Q3. What does 'System.out.println(1 << 32);' print in Java?
(जावा में 'System.out.println(1 << 32);' क्या प्रिंट करता है?)
(A) 4294967296
(B) 1
(C) 0
(D) Compile-time shift overflow error
Correct Answer: (B)
[EN] For a 32-bit int left operand, Java masks the shift distance with 0x1F (5 bits: 32 & 31 = 0), so 1 << 32 performs a 0-bit shift and returns 1.
[HI] 32-बिट int बाएँ ऑपरेंड के लिए जावा शिफ्ट दूरी को 31 (5 बिट्स) से मास्क करता है; अतः 32 & 31 = 0 शिफ्ट होकर परिणाम 1 आता है।
Q4. What is the value of (7 << 3) in Java?
(जावा में (7 << 3) का मान क्या होगा?)
(A) 21
(B) 0
(C) 56
(D) 10
Correct Answer: (C)
[EN] Left shifting 7 by 3 bits multiplies by 2^3 (8): 7 * 8 = 56 (binary 00000111 becomes 00111000).
[HI] 7 को 3 बिट बाईं ओर शिफ्ट करना 2^3 (8) से गुणा करने जैसा है: 7 * 8 = 56।
Q5. Which of the following is NOT a valid Java operator?
(निम्नलिखित में से कौन सा जावा में एक वैध ऑपरेटर नहीं है?)
(A) <<<
(B) >>
(C) <<
(D) >>>
Correct Answer: (A)
[EN] Java provides <<, >>, and >>>, along with compound assignments <<=, >>=, and >>>=, but has no '<<<' operator.
[HI] जावा में <<, >> और >>> उपलब्ध हैं, परंतु '<<<' नाम का कोई ऑपरेटर नहीं होता।
Q6. What does 'System.out.println(-1 >> 15);' evaluate to in Java?
(जावा में 'System.out.println(-1 >> 15);' का मान क्या होगा?)
(A) -32768
(B) 65535
(C) 0
(D) -1
Correct Answer: (D)
[EN] -1 has all 32 bits equal to 1; signed right shift >> refills every vacated left bit with the sign bit (1), keeping all 32 bits as 1 (-1).
[HI] -1 के सभी 32 बिट्स 1 होते हैं; साइन्ड राइट शिफ्ट >> खाली बाएँ स्थानों पर भी साइन बिट 1 भरता है, जिससे मान हमेशा -1 बना रहता है।
Q7. Why is 'int mid = (low + high) >>> 1;' preferred over '(low + high) / 2' in binary search?
(बाइनरी सर्च में '(low + high) / 2' के बजाय 'int mid = (low + high) >>> 1;' को प्राथमिकता क्यों दी जाती है?)
(A) Division by 2 throws ArithmeticException on odd sums
(B) It automatically rounds mid upward to the ceiling
(C) >>> automatically handles floating-point indices
(D) It avoids negative index bugs if (low + high) overflows Integer.MAX_VALUE into the sign bit
Correct Answer: (D)
[EN] When low + high exceeds Integer.MAX_VALUE, signed addition wraps into a negative number (MSB=1). Signed '/ 2' yields a negative index, whereas '>>> 1' shifts in 0 at the MSB, producing the exact unsigned average.
[HI] जब low + high Integer.MAX_VALUE से बड़ा होकर ऋणात्मक हो जाता है, तो '/ 2' नेगेटिव इंडेक्स दे देता है, जबकि '>>> 1' साइन बिट पर 0 भरकर सही मध्य इंडेक्स देता है।
Q8. What happens when shifting a 'byte' variable: byte b = -1; System.out.println(b >>> 1);
(byte वेरिएबल को शिफ्ट करने पर क्या होता है: byte b = -1; System.out.println(b >>> 1);)
(A) Prints -1
(B) Compile-time error: >>> cannot be used on byte
(C) Prints 2147483647 because b is promoted to 32-bit int before shifting
(D) Prints 127 because byte is 8 bits
Correct Answer: (C)
[EN] Unary numeric promotion widens byte b (-1) to 32-bit int 0xFFFFFFFF before >>> executes, resulting in 0x7FFFFFFF = 2147483647.
[HI] शिफ्ट से पहले byte b (-1) का 32-बिट int (0xFFFFFFFF) में प्रमोशन हो जाता है, जिससे >>> 1 चलाने पर 0x7FFFFFFF (2147483647) प्रिंट होता है।
Q9. What is the output of: System.out.println(1L << 64);
(System.out.println(1L << 64); का आउटपुट क्या होगा?)
(A) 1
(B) 0
(C) 2
(D) 18446744073709551616
Correct Answer: (A)
[EN] For a 64-bit long left operand, Java uses only the lower 6 bits of the shift count (64 & 63 = 0), yielding 1L << 0 = 1.
[HI] 64-बिट long ऑपरेंड के लिए जावा शिफ्ट दूरी के केवल निचले 6 बिट्स (64 & 63 = 0) उपयोग करता है, जिससे परिणाम 1 आता है।
Q10. What is the output of: System.out.println(1 << 31);
(System.out.println(1 << 31); का आउटपुट क्या होगा?)
(A) -2147483648
(B) 2147483648
(C) 0
(D) 1
Correct Answer: (A)
[EN] Shifting 1 left by 31 bits places the 1 in the sign bit (0x80000000), which represents Integer.MIN_VALUE (-2147483648) in signed 32-bit int.
[HI] 1 को 31 बिट बाएँ खिसकाने पर 1 सबसे बाएँ साइन बिट (0x80000000) में पहुंच जाता है, जो 32-बिट साइन्ड int में -2147483648 है।
Q11. Which shift operation gives the exact same result as (24 >> 2) for positive int 24?
(धनात्मक int 24 के लिए कौन सा शिफ्ट व्यंजक (24 >> 2) के बिल्कुल समान परिणाम देता है?)
(A) 24 >> 3
(B) 24 >>> 2
(C) 24 << 2
(D) 24 >>> 4
Correct Answer: (B)
[EN] For non-negative numbers (MSB = 0), signed right shift (>>) and unsigned right shift (>>>) both insert 0s on the left and yield 24 / 4 = 6.
[HI] धनात्मक संख्याओं (MSB = 0) के लिए साइन्ड (>>) और अनसाइन्ड (>>>) दोनों बाईं ओर 0 भरते हैं और समान मान 6 देते हैं।
Q12. What happens if you apply a shift operator to a 'double' variable like '16.0 >> 1'?
(यदि आप '16.0 >> 1' की तरह किसी 'double' मान पर शिफ्ट ऑपरेटर लागू करें तो क्या होगा?)
(A) Throws ArithmeticException at runtime
(B) Outputs 8.0
(C) Compile-time error: bad operand types for binary operator '>>'
(D) Outputs 8
Correct Answer: (C)
[EN] Shift operators in Java are defined exclusively for integer primitive types (byte, short, int, long, char), never for float, double, or boolean.
[HI] जावा में शिफ्ट ऑपरेटर्स केवल पूर्णांक प्रकारों (byte, short, int, long, char) पर मान्य हैं; float, double या boolean पर ये कंपाइल त्रुटि देते हैं।
Q13. What is the value of (16 >> 1) + (4 << 2)?
((16 >> 1) + (4 << 2) का मान क्या होगा?)
(A) 40
(B) 24
(C) 16
(D) 12
Correct Answer: (B)
[EN] 16 >> 1 = 16 / 2 = 8, and 4 << 2 = 4 * 4 = 16; adding them gives 8 + 16 = 24.
[HI] 16 >> 1 = 8 और 4 << 2 = 16 होता है; दोनों का योग 8 + 16 = 24 होगा।
Q14. Which compound assignment operator performs an unsigned right shift and assigns the result back?
(कौन सा कंपाउंड असाइनमेंट ऑपरेटर अनसाइन्ड राइट शिफ्ट करके परिणाम वापस असाइन करता है?)
(A) >>>=
(B) <<<=
(C) >>=
(D) <<=
Correct Answer: (A)
[EN] Java supports <<=, >>=, and >>>= as valid bitwise shift compound assignment operators.
[HI] जावा में <<=, >>= और >>>= तीनों वैध बिटवाइज़ शिफ्ट कंपाउंड असाइनमेंट ऑपरेटर हैं।
Q15. What is the result of: int x = 1; System.out.println(x << -1);
(int x = 1; System.out.println(x << -1); का परिणाम क्या होगा?)
(A) Compile-time error for negative shift distance
(B) -2147483648
(C) 0
(D) 1
Correct Answer: (B)
[EN] A negative shift count like -1 (0xFFFFFFFF) is masked with 0x1F (lowest 5 bits = 31), so 1 << -1 executes as 1 << 31 = Integer.MIN_VALUE (-2147483648).
[HI] -1 को 0x1F से मास्क करने पर निचले 5 बिट्स 31 बनते हैं, अतः 1 << -1 वास्तव में 1 << 31 अर्थात -2147483648 देता है।
Q16. Which statement about Java's shift operators is FALSE?
(जावा के शिफ्ट ऑपरेटर्स के बारे में निम्नलिखित में से कौन सा कथन असत्य है?)
(A) For any negative int x, (x >> 1) and (x >>> 1) produce the exact same value
(B) A char operand on the left side of << is automatically promoted to int
(C) The right-hand shift distance is masked to 5 bits for int and 6 bits for long
(D) Shift operators have lower precedence than + and - arithmetic operators
Correct Answer: (A)
[EN] For negative ints (MSB = 1), >> fills left bits with 1 (staying negative) whereas >>> fills left bits with 0 (becoming positive), so their results always differ.
[HI] ऋणात्मक संख्याओं के लिए >> बाईं ओर 1 भरता है (ऋणात्मक रहता है) जबकि >>> बाईं ओर 0 भरता है (धनात्मक हो जाता है), इसलिए उनके परिणाम अलग होते हैं।
Q17. Within Shift Operators in Java: << (Left), >> (Signed Right), >>> (Unsigned Right), which shift operator / rule directly maps to: "Shifts left; fills trailing right bits with 0"?
(Shift Operators in Java: << (Left), >> (Signed Right), >>> (Unsigned Right) में कौन सा Shift Operator / Rule सीधे "Shifts left; fills trailing right bits with 0" से संबंधित है?)
(A) >> (Signed Right)
(B) >>> (Unsigned Right)
(C) Shift Distance Mask
(D) << (Left Shift)
Correct Answer: (D)
[EN] In Shift Operators in Java: << (Left), >> (Signed Right), >>> (Unsigned Right), '<< (Left Shift)' handles 'Shifts left; fills trailing right bits with 0' (Multiplies by 2^n without overflow; no <<< exists in Java).
[HI] Shift Operators in Java: << (Left), >> (Signed Right), >>> (Unsigned Right) में '<< (Left Shift)' का कार्य/विवरण 'Shifts left; fills trailing right bits with 0' (Multiplies by 2^n without overflow; no <<< exists in Java) है।
Q18. Which option accurately describes '>> (Signed Right)' in Shift Operators in Java: << (Left), >> (Signed Right), >>> (Unsigned Right)?
(Shift Operators in Java: << (Left), >> (Signed Right), >>> (Unsigned Right) के संदर्भ में '>> (Signed Right)' की सही विशेषता कौन सी है?)
(A) Shifts right; preserves sign bit (MSB) on left
(B) Shifts left; fills trailing right bits with 0
(C) Shifts right; always fills leading left bits with 0
(D) Uses lower 5 bits (int) or 6 bits (long) of count
Correct Answer: (A)
[EN] '>> (Signed Right)' provides 'Shifts right; preserves sign bit (MSB) on left' — key point: Maintains negative sign; -5 >> 1 evaluates to -3.
[HI] '>> (Signed Right)' का मुख्य विवरण 'Shifts right; preserves sign bit (MSB) on left' है (Maintains negative sign; -5 >> 1 evaluates to -3)।
Q19. In Shift Operators in Java: << (Left), >> (Signed Right), >>> (Unsigned Right), identify the correct parameter for the specification: "Shifts right; always fills leading left bits with 0".
(Shift Operators in Java: << (Left), >> (Signed Right), >>> (Unsigned Right) में "Shifts right; always fills leading left bits with 0" विवरण किस विकल्प के लिए सही है?)
(A) << (Left Shift)
(B) >>> (Unsigned Right)
(C) >> (Signed Right)
(D) Shift Distance Mask
Correct Answer: (B)
[EN] '>>> (Unsigned Right)' corresponds to 'Shifts right; always fills leading left bits with 0' (Turns negative int positive; -1 >>> 1 == Integer.MAX_VALUE).
[HI] '>>> (Unsigned Right)' का संबंध 'Shifts right; always fills leading left bits with 0' (Turns negative int positive; -1 >>> 1 == Integer.MAX_VALUE) से है।
Q20. Considering Shift Operators in Java: << (Left), >> (Signed Right), >>> (Unsigned Right), what key behavior defines 'Shift Distance Mask'?
(Shift Operators in Java: << (Left), >> (Signed Right), >>> (Unsigned Right) में 'Shift Distance Mask' की मुख्य भूमिका या मान क्या है?)
(A) Shifts right; preserves sign bit (MSB) on left
(B) Shifts left; fills trailing right bits with 0
(C) Shifts right; always fills leading left bits with 0
(D) Uses lower 5 bits (int) or 6 bits (long) of count
Correct Answer: (D)
[EN] 'Shift Distance Mask' represents 'Uses lower 5 bits (int) or 6 bits (long) of count' (1 << 32 equals 1; 1L << 64 equals 1L).
[HI] 'Shift Distance Mask' का अर्थ/मान 'Uses lower 5 bits (int) or 6 bits (long) of count' है (1 << 32 equals 1; 1L << 64 equals 1L)।
Shift Operators in Java: << (Left), >> (Signed Right), >>> (Unsigned Right) — Exam FAQs
Q1: Why doesn't Java have an unsigned left shift operator (<<<)?
जावा में अनसाइन्ड लेफ्ट शिफ्ट ऑपरेटर (<<<) क्यों नहीं होता?
[EN] Left shifting moves bits toward the MSB and always inserts 0s into the vacated rightmost positions regardless of whether the number is signed or unsigned. Hence << already performs both logical and arithmetic left shifts identically.
[HI] लेफ्ट शिफ्ट में खाली होने वाले दाएँ स्थानों पर संख्या के चिह्न की परवाह किए बिना हमेशा 0 ही भरा जाता है। इसलिए लॉजिकल और अरिथमेटिक लेफ्ट शिफ्ट समान होने के कारण अलग <<< की आवश्यकता नहीं होती।
Q2: What happens if you shift by a negative number like '8 >> -1' in Java?
जावा में '8 >> -1' जैसी ऋणात्मक दूरी से शिफ्ट करने पर क्या होता है?
[EN] Java does not shift in the opposite direction or throw an error. Instead, it masks the shift distance with 0x1F for int (-1 & 31 == 31) or 0x3F for long (-1 & 63 == 63), so 8 >> -1 becomes 8 >> 31 = 0.
[HI] जावा विपरीत दिशा में शिफ्ट नहीं करता और न ही एरर देता है। बल्कि वह int के लिए दूरी के केवल निचले 5 बिट्स (-1 & 31 = 31) लेता है, अतः 8 >> -1 वास्तव में 8 >> 31 के रूप में चलता है।
Q3: Why does (x << 1) have lower precedence than (x + 1) in Java?
जावा में (x << 1) की प्राथमिकता (x + 1) से कम क्यों रखी गई है?
[EN] Like C and C++, Java places shift operators (<<, >>, >>>) just below additive operators (+, -). Therefore '1 << 2 + 1' evaluates as '1 << (2 + 1) = 8' rather than '(1 << 2) + 1 = 5'; always parenthesize shifts inside arithmetic expressions.
[HI] C और C++ की तरह जावा में शिफ्ट ऑपरेटर्स की प्राथमिकता + और - से नीचे है। इसलिए '1 << 2 + 1' का अर्थ '1 << (2 + 1) = 8' होता है; अतः कोष्ठक (parentheses) लगाना सुरक्षित रहता है।
Post a Comment
Post a Comment