PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Java Operators: Arithmetic, Relational, Logical, Bitwise (जावा ऑपरेटर्स: अंकगणितीय, रिलेशनल, लॉजिकल और बिटवाइज़) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Java provides rich operator families—Arithmetic (+, -, *, /, %), Relational (==, !=, <, >, <=, >=), Logical (&&, ||, !), and Bitwise (&, |, ^, ~)—governed by strict operand promotion and precedence rules. Key Java behaviors include short-circuit evaluation in logical operators, ArithmeticException on integer zero division, and non-short-circuit boolean evaluation when using & and |.
अध्याय सार: जावा में सख्त ऑपरेंड प्रमोशन और प्राथमिकता नियमों से नियंत्रित समृद्ध ऑपरेटर समूह—अंकगणितीय (+, -, *, /, %), रिलेशनल (==, !=, <, >, <=, >=), लॉजिकल (&&, ||, !) और बिटवाइज़ (&, |, ^, ~)—उपलब्ध हैं। इनमें लॉजिकल ऑपरेटर्स का शॉर्ट-सर्किट मूल्यांकन, पूर्णांक शून्य विभाजन पर ArithmeticException तथा & और | द्वारा नॉन-शॉर्ट-सर्किट बूलियन जांच प्रमुख विशेषताएँ हैं।
Java Operators: Arithmetic, Relational, Logical, Bitwise: Detailed Notes & Architecture (जावा ऑपरेटर्स: अंकगणितीय, रिलेशनल, लॉजिकल और बिटवाइज़)
1. Arithmetic Operators, Integer Division, and Modulus Behavior (अंकगणितीय ऑपरेटर्स, पूर्णांक विभाजन और मॉड्यूलस नियम)
[EN] Java's binary arithmetic operators (+, -, *, /, %) perform standard math alongside unary increment (++) and decrement (--). Integer division truncates fractions toward zero (7 / 2 == 3, -7 / 2 == -3) and throws java.lang.ArithmeticException when dividing or taking modulus by integer 0, whereas floating-point division yields IEEE 754 Infinity or NaN without throwing. Unlike C/C++, Java's remainder operator (%) works on both integers and floating-point values (5.5 % 2.0 == 1.5), and the result always takes the sign of the left-hand dividend (-7 % 3 == -1). Note also that '+' is overloaded for String concatenation left-to-right: 1 + 2 + "Java" yields "3Java", whereas "Java" + 1 + 2 yields "Java12".
[HI] जावा के बाइनरी अंकगणितीय ऑपरेटर्स (+, -, *, /, %) के साथ यूनरी इंक्रीमेंट (++) और डिक्रीमेंट (--) प्रयुक्त होते हैं। पूर्णांक विभाजन दशमलव भाग को शून्य की ओर काटता है (7 / 2 == 3) और पूर्णांक 0 से भाग या मॉड्यूलस देने पर ArithmeticException फेंकता है, जबकि फ़्लोटिंग-पॉइंट विभाजन बिना एरर के Infinity या NaN देता है। C/C++ के विपरीत, जावा में शेषफल ऑपरेटर (%) पूर्णांक और दशमलव दोनों पर काम करता है (5.5 % 2.0 == 1.5), और परिणाम का चिह्न हमेशा बाएँ भाज्य (dividend) के चिह्न जैसा रहता है (-7 % 3 == -1)। '+' ऑपरेटर बाएँ-से-दाएँ स्ट्रिंग संयोजन भी करता है: 1 + 2 + "Java" का मान "3Java" होगा, जबकि "Java" + 1 + 2 का मान "Java12" होगा।
2. Relational (Comparison) Operators and Reference Identity (रिलेशनल (तुलनात्मक) ऑपरेटर्स और रेफरेंस पहचान)
[EN] The six relational operators (<, <=, >, >=, ==, !=) always evaluate to a boolean result (true or false) and cannot be chained like math inequalities (writing 1 < x < 10 fails to compile). Ordering operators (<, <=, >, >=) accept numeric primitives (including char) and never booleans or object references. Equality operators (== and !=) work on primitives (comparing values), booleans (comparing true/false), and object references (comparing memory identity rather than object content). Comparing an integer and a floating-point primitive with == (such as 5 == 5.0) performs numeric widening and returns true.
[HI] छह रिलेशनल ऑपरेटर्स (<, <=, >, >=, ==, !=) हमेशा boolean (true या false) परिणाम देते हैं और गणित की तरह श्रृंखला में (1 < x < 10) नहीं लिखे जा सकते। क्रम तुलना ऑपरेटर्स (<, <=, >, >=) केवल संख्यात्मक प्रिमिटिव (char सहित) पर कार्य करते हैं, boolean या ऑब्जेक्ट रेफरेंस पर नहीं। समानता ऑपरेटर्स (== और !=) प्रिमिटिव में मान की तुलना करते हैं, जबकि ऑब्जेक्ट रेफरेंस में वे सामग्री के बजाय मेमोरी पते (identity) की जांच करते हैं। int और double की तुलना (जैसे 5 == 5.0) करने पर स्वचालित वाइडनिंग होकर परिणाम true आता है।
3. Logical Operators and Short-Circuit Evaluation (लॉजिकल ऑपरेटर्स और शॉर्ट-सर्किट मूल्यांकन)
[EN] Java's conditional logical operators—AND (&&), OR (||), and unary NOT (!)—accept strictly boolean operands and always produce a boolean value. Both && and || employ short-circuit evaluation: in 'false && expr', Java skips evaluating expr because the result is guaranteed false; in 'true || expr', Java skips expr because the result is guaranteed true. This short-circuit property makes null-guard and bounds-guard idioms like 'if (str != null && str.length() > 0)' completely safe against NullPointerException. In contrast, single '&' and '|' used on booleans evaluate BOTH left and right expressions unconditionally before computing the boolean result.
[HI] जावा के कंडीशनल लॉजिकल ऑपरेटर्स—AND (&&), OR (||) और NOT (!)—केवल boolean ऑपरेंड स्वीकार करते हैं और boolean परिणाम देते हैं। && और || दोनों शॉर्ट-सर्किट मूल्यांकन का उपयोग करते हैं: 'false && expr' में दायाँ व्यंजक निष्पादित नहीं होता और 'true || expr' में भी दायाँ भाग छोड़ दिया जाता है। इसी विशेषता के कारण 'if (str != null && str.length() > 0)' जैसे गार्ड कोड NullPointerException से पूरी तरह सुरक्षित रहते हैं। इसके विपरीत, boolean पर सिंगल '&' और '|' दोनों पक्षों को हर हाल में निष्पादित करते हैं।
4. Bitwise Operators and Operator Precedence (बिटवाइज़ ऑपरेटर्स और ऑपरेटर प्राथमिकता)
[EN] Bitwise operators—AND (&), OR (|), XOR (^), and unary complement (~)—manipulate individual binary bits of integer types (byte, short, int, long, and char), after promoting types smaller than int to int. Bitwise AND (&) yields 1 only if both bits are 1; OR (|) yields 1 if either bit is 1; XOR (^) yields 1 when corresponding bits differ (useful for toggling bits or swapping two variables without a temporary variable). Unary bitwise complement (~) flips every bit (0 to 1, 1 to 0); in two's-complement arithmetic, ~x always equals -(x + 1), so ~5 == -6 and ~(-1) == 0. Among precedence rules, unary (!, ~, ++, --) binds tightest, followed by multiplicative (*, /, %), additive (+, -), relational, bitwise (& > ^ > |), logical (&& > ||), ternary (?:), and assignment (=).
[HI] बिटवाइज़ ऑपरेटर्स—AND (&), OR (|), XOR (^) और यूनरी कॉम्प्लिमेंट (~)—इंटीजर प्रकारों (byte, short, int, long, char) के प्रत्येक बिट पर कार्य करते हैं, जहाँ int से छोटे ऑपरेंड पहले int में प्रमोट होते हैं। बिटवाइज़ AND (&) दोनों बिट 1 होने पर 1 देता है, OR (|) किसी भी बिट के 1 होने पर 1 देता है, तथा XOR (^) दोनों बिट भिन्न होने पर 1 देता है। यूनरी बिटवाइज़ कॉम्प्लिमेंट (~) सभी बिट्स पलट देता है; टूज़-कॉम्प्लिमेंट में ~x हमेशा -(x + 1) के बराबर होता है (जैसे ~5 == -6)। प्राथमिकता क्रम में यूनरी सबसे ऊपर, फिर * / %, फिर + -, फिर रिलेशनल, फिर बिटवाइज़ (& > ^ > |), फिर लॉजिकल (&& > ||), फिर ?: और अंत में असाइनमेंट (=) आते हैं।
Syntax, Formula & Technical Example (Java Operators: Arithmetic, Relational, Logical, Bitwise)
public class OperatorsDemo {
public static void main(String[] args) {
int a = 5, b = 3; // Binary: 5=0101, 3=0011
System.out.println("Int div: " + (7 / 2) + ", Mod: " + (-7 % 3));
System.out.println("AND=" + (a & b) + ", OR=" + (a | b) + ", XOR=" + (a ^ b) + ", ~5=" + (~a));
String s = null;
boolean safe = (s != null) && (s.length() > 0); // Short-circuit prevents NPE
System.out.println("Safe=" + safe + ", Concat=" + (1 + 2 + "Java"));
}
}
Java Operators: Arithmetic, Relational, Logical, Bitwise Quick Reference & Comparison Table
| Operator Category | Representative Operators | Key Exam Fact |
|---|---|---|
| Arithmetic | +, -, *, /, %, ++, -- | Integer / 0 throws ArithmeticException; % supports floats |
| Relational | ==, !=, <, >, <=, >= | Always returns boolean; == on objects compares references |
| Logical | &&, ||, ! | Short-circuits right-hand expression; boolean operands only |
| Bitwise | &, |, ^, ~ | Operates bit-by-bit on integers; ~x equals -(x + 1) |
Java Operators: Arithmetic, Relational, Logical, Bitwise Top 20 Bilingual MCQs with Answers
Q1. What is the exact output of: System.out.println(~10);
(System.out.println(~10); का सटीक आउटपुट क्या होगा?)
(A) -10
(B) -9
(C) 11
(D) -11
Correct Answer: (D)
[EN] The bitwise complement operator ~ inverts all 32 bits of 10 in two's complement, where ~x always equals -(x + 1), yielding -11.
[HI] बिटवाइज़ कॉम्प्लिमेंट ऑपरेटर ~ टूज़-कॉम्प्लिमेंट में 10 के सभी 32 बिट्स पलट देता है; ~x हमेशा -(x + 1) अर्थात -11 के बराबर होता है।
Q2. What is the output of: int x = 5; System.out.println(x++ + ++x);
(int x = 5; System.out.println(x++ + ++x); का आउटपुट क्या होगा?)
(A) 11
(B) 10
(C) 13
(D) 12
Correct Answer: (D)
[EN] Left-to-right evaluation: x++ returns 5 and increments x to 6; then ++x increments x to 7 and returns 7; 5 + 7 = 12.
[HI] बाएँ-से-दाएँ मूल्यांकन में x++ पहले 5 देता है और x को 6 करता है; फिर ++x मान को 7 करके 7 देता है; अतः 5 + 7 = 12 प्रिंट होता है।
Q3. What happens when you execute: int x = 0; boolean res = (false && (++x > 0)); System.out.println(x);
(int x = 0; boolean res = (false && (++x > 0)); System.out.println(x); चलाने पर क्या प्रिंट होगा?)
(A) Compile-time error
(B) 0
(C) 1
(D) false
Correct Answer: (B)
[EN] Because the left operand of && is false, short-circuit evaluation skips evaluating (++x > 0) entirely, so x stays 0.
[HI] चूंकि && का बायाँ पक्ष false है, इसलिए शॉर्ट-सर्किट मूल्यांकन दाएँ भाग (++x > 0) को निष्पादित ही नहीं करता और x का मान 0 रहता है।
Q4. What happens if single '&' is used instead of '&&' in: int x = 0; boolean res = (false & (++x > 0)); System.out.println(x);
(यदि '&&' के स्थान पर सिंगल '&' लिखा जाए: int x = 0; boolean res = (false & (++x > 0)); System.out.println(x); तो आउटपुट क्या होगा?)
(A) 0
(B) 1
(C) Compile-time error because & only works on ints
(D) true
Correct Answer: (B)
[EN] When applied to boolean operands, '&' acts as a non-short-circuit boolean AND operator, evaluating both sides so ++x increments x to 1.
[HI] boolean ऑपरेंड पर '&' नॉन-शॉर्ट-सर्किट AND की तरह कार्य करता है और दोनों पक्षों का मूल्यांकन करता है, जिससे x बढ़कर 1 हो जाता है।
Q5. What is the output of: System.out.println(-10 % 3);
(System.out.println(-10 % 3); का आउटपुट क्या होगा?)
(A) 2
(B) -1
(C) 1
(D) -2
Correct Answer: (B)
[EN] In Java, the modulus operator % preserves the sign of the dividend (-10): -10 = (-3)*3 + (-1), so -10 % 3 equals -1.
[HI] जावा में मॉड्यूलस ऑपरेटर % भाज्य (-10) का चिह्न बनाए रखता है: -10 = (-3)*3 + (-1), अतः परिणाम -1 होता है।
Q6. Which expression evaluates to the string "102030" in Java?
(जावा में निम्नलिखित में से किस व्यंजक का परिणाम "102030" होगा?)
(A) "10" + 20 + 30
(B) 10 + "20" + 30
(C) 10 + 20 + "30"
(D) "" + (10 + 20) + 30
Correct Answer: (A)
[EN] Evaluating left-to-right, "10" + 20 concatenates to "1020", and "1020" + 30 concatenates to "102030", whereas 10 + 20 + "30" yields "3030".
[HI] बाएँ-से-दाएँ मूल्यांकन में "10" + 20 पहले "1020" बनता है और फिर + 30 से "102030" बनता है, जबकि 10 + 20 + "30" का मान "3030" होता है।
Q7. What is the result of evaluating: 5.0 / 0 in Java?
(जावा में 5.0 / 0 का मूल्यांकन करने पर क्या परिणाम आता है?)
(A) NaN
(B) Infinity (Double.POSITIVE_INFINITY)
(C) Throws ArithmeticException
(D) 0.0
Correct Answer: (B)
[EN] Unlike integer 5 / 0 (which throws ArithmeticException), floating-point division by zero follows IEEE 754 and returns Infinity.
[HI] पूर्णांक 5 / 0 (जो ArithmeticException फेंकता है) के विपरीत, फ़्लोटिंग-पॉइंट में शून्य से भाग IEEE 754 के अनुसार Infinity देता है।
Q8. What is the value of (6 ^ 3) in Java using bitwise XOR?
(जावा में बिटवाइज़ XOR द्वारा (6 ^ 3) का मान क्या होगा?)
(A) 5
(B) 7
(C) 9
(D) 2
Correct Answer: (A)
[EN] 6 in binary is 0110 and 3 is 0011; XOR produces 1 where bits differ: 0101 in binary = 5.
[HI] बाइनरी में 6 = 0110 और 3 = 0011 होता है; भिन्न बिट्स पर 1 देने वाला XOR परिणाम 0101 अर्थात 5 देता है।
Q9. Why is 'if (5 < x < 10)' a compile-time error in Java?
(जावा में 'if (5 < x < 10)' कंपाइल-टाइम त्रुटि क्यों देता है?)
(A) Java does not allow two '<' operators on one line
(B) 10 must be written before x
(C) 5 < x evaluates to a boolean, which cannot be compared with 10 using '<'
(D) x must be declared float for chaining comparisons
Correct Answer: (C)
[EN] Relational operators associate left-to-right: (5 < x) returns a boolean, and 'boolean < int' is invalid in Java; write '5 < x && x < 10'.
[HI] (5 < x) का परिणाम boolean होता है जिसकी तुलना '< 10' से नहीं की जा सकती; सही रूप '5 < x && x < 10' है।
Q10. Which operator can swap two integer variables a and b without using a temporary third variable?
(बिना किसी अस्थायी तीसरे वेरिएबल के दो इंटीजर वेरिएबल्स a और b के मान आपस में बदलने (swap) के लिए किस बिटवाइज़ ऑपरेटर का उपयोग किया जा सकता है?)
(A) | (Bitwise OR)
(B) & (Bitwise AND)
(C) ~ (Bitwise NOT)
(D) ^ (Bitwise XOR)
Correct Answer: (D)
[EN] The XOR swap sequence 'a = a ^ b; b = a ^ b; a = a ^ b;' swaps distinct integer variables without overflow or temporary storage.
[HI] XOR स्वैप अनुक्रम 'a = a ^ b; b = a ^ b; a = a ^ b;' बिना किसी अस्थायी वेरिएबल या ओवरफ़्लो के दोनों मान बदल देता है।
Q11. Which expression cleanly tests whether an integer n is odd using a bitwise operator?
(बिटवाइज़ ऑपरेटर का उपयोग करके यह जांचने का सही व्यंजक कौन सा है कि पूर्णांक n विषम (odd) है?)
(A) (n ^ 1) == 0
(B) (n & 1) == 1
(C) (n | 1) == 1
(D) (n ~ 1) == 1
Correct Answer: (B)
[EN] An integer's least significant bit (LSB) is 1 if and only if the number is odd (works reliably for negative integers too, unlike n % 2 == 1).
[HI] किसी भी पूर्णांक का सबसे दायाँ (LSB) बिट तभी 1 होता है जब वह संख्या विषम हो; (n & 1) == 1 ऋणात्मक विषम संख्याओं पर भी सही काम करता है।
Q12. Among the operators &&, ||, &, and ==, which has the HIGHEST precedence in Java?
(जावा में &&, ||, & और == ऑपरेटर्स में से किसकी प्राथमिकता (precedence) सबसे अधिक है?)
(A) ||
(B) &
(C) ==
(D) &&
Correct Answer: (C)
[EN] Java precedence order from highest to lowest among these four is: == (equality) > & (bitwise AND) > && (logical AND) > || (logical OR).
[HI] इन चारों में जावा प्राथमिकता क्रम उच्च से निम्न की ओर == > & > && > || है।
Q13. What does 'System.out.println(12 | 10);' output?
('System.out.println(12 | 10);' का आउटपुट क्या होगा?)
(A) 14
(B) 8
(C) 6
(D) 22
Correct Answer: (A)
[EN] 12 is 1100 and 10 is 1010 in binary; bitwise OR gives 1110, which equals 14.
[HI] बाइनरी में 12 = 1100 और 10 = 1010 है; बिटवाइज़ OR से 1110 अर्थात 14 प्राप्त होता है।
Q14. Which operator is NOT applicable to 'boolean' operands in Java?
(जावा में निम्नलिखित में से कौन सा ऑपरेटर 'boolean' ऑपरेंड पर लागू नहीं होता है?)
(A) ^ (XOR)
(B) ~ (Bitwise Complement)
(C) & (AND)
(D) != (Not Equal)
Correct Answer: (B)
[EN] Boolean values support !, &, |, ^, &&, ||, ==, and !=, whereas bitwise complement (~) applies exclusively to integral numeric types.
[HI] boolean मानों पर !, &, |, ^, &&, ||, == और != मान्य हैं, जबकि बिटवाइज़ कॉम्प्लिमेंट (~) केवल पूर्णांक प्रकारों पर काम करता है।
Q15. What happens when you evaluate: int a = 10; System.out.println(a > 5 ? "Yes" : 0);
(int a = 10; System.out.println(a > 5 ? "Yes" : 0); का मूल्यांकन करने पर क्या होता है?)
(A) Compile-time error because branches return String and int
(B) Throws ClassCastException at runtime
(C) Prints 0
(D) Compiles and prints "Yes" (ternary result type resolves to Object/Serializable)
Correct Answer: (D)
[EN] In Java, System.out.println(Object) accepts the common supertype (Object) of String and boxed Integer; since a > 5 is true, "Yes" is printed.
[HI] println(Object) के संदर्भ में String और बॉक्स्ड Integer का साझा सुपरटाइप Object बनता है; चूंकि a > 5 सत्य है, यह "Yes" प्रिंट करता है।
Q16. Which statement about Java operators is INCORRECT?
(जावा ऑपरेटर्स के बारे में निम्नलिखित में से कौन सा कथन गलत है?)
(A) Equality operator == compares memory addresses when used on two String objects
(B) The modulus operator % works with double and float operands
(C) Logical NOT (!) has higher precedence than logical AND (&&)
(D) Java supports user-defined operator overloading for custom classes like Matrix
Correct Answer: (D)
[EN] Java intentionally prohibits user-defined operator overloading (only built-in '+' for String concatenation is special-cased in the language).
[HI] जावा जानबूझकर यूज़र-डिफ़ाइंड ऑपरेटर ओवरलोडिंग की अनुमति नहीं देता (केवल बिल्ट-इन '+' स्ट्रिंग संयोजन के लिए अपवाद है)।
Q17. Within Java Operators: Arithmetic, Relational, Logical, Bitwise, which operator category directly maps to: "+, -, *, /, %, ++, --"?
(Java Operators: Arithmetic, Relational, Logical, Bitwise में कौन सा Operator Category सीधे "+, -, *, /, %, ++, --" से संबंधित है?)
(A) Relational
(B) Arithmetic
(C) Logical
(D) Bitwise
Correct Answer: (B)
[EN] In Java Operators: Arithmetic, Relational, Logical, Bitwise, 'Arithmetic' handles '+, -, *, /, %, ++, --' (Integer / 0 throws ArithmeticException; % supports floats).
[HI] Java Operators: Arithmetic, Relational, Logical, Bitwise में 'Arithmetic' का कार्य/विवरण '+, -, *, /, %, ++, --' (Integer / 0 throws ArithmeticException; % supports floats) है।
Q18. Which option accurately describes 'Relational' in Java Operators: Arithmetic, Relational, Logical, Bitwise?
(Java Operators: Arithmetic, Relational, Logical, Bitwise के संदर्भ में 'Relational' की सही विशेषता कौन सी है?)
(A) &&, ||, !
(B) &, |, ^, ~
(C) +, -, *, /, %, ++, --
(D) ==, !=, <, >, <=, >=
Correct Answer: (D)
[EN] 'Relational' provides '==, !=, <, >, <=, >=' — key point: Always returns boolean; == on objects compares references.
[HI] 'Relational' का मुख्य विवरण '==, !=, <, >, <=, >=' है (Always returns boolean; == on objects compares references)।
Q19. In Java Operators: Arithmetic, Relational, Logical, Bitwise, identify the correct parameter for the specification: "&&, ||, !".
(Java Operators: Arithmetic, Relational, Logical, Bitwise में "&&, ||, !" विवरण किस विकल्प के लिए सही है?)
(A) Relational
(B) Bitwise
(C) Arithmetic
(D) Logical
Correct Answer: (D)
[EN] 'Logical' corresponds to '&&, ||, !' (Short-circuits right-hand expression; boolean operands only).
[HI] 'Logical' का संबंध '&&, ||, !' (Short-circuits right-hand expression; boolean operands only) से है।
Q20. Considering Java Operators: Arithmetic, Relational, Logical, Bitwise, what key behavior defines 'Bitwise'?
(Java Operators: Arithmetic, Relational, Logical, Bitwise में 'Bitwise' की मुख्य भूमिका या मान क्या है?)
(A) &, |, ^, ~
(B) ==, !=, <, >, <=, >=
(C) +, -, *, /, %, ++, --
(D) &&, ||, !
Correct Answer: (A)
[EN] 'Bitwise' represents '&, |, ^, ~' (Operates bit-by-bit on integers; ~x equals -(x + 1)).
[HI] 'Bitwise' का अर्थ/मान '&, |, ^, ~' है (Operates bit-by-bit on integers; ~x equals -(x + 1))।
Java Operators: Arithmetic, Relational, Logical, Bitwise — Exam FAQs
Q1: What is the difference between & and && (and | vs ||) in Java?
जावा में & बनाम && (तथा | बनाम ||) में क्या अंतर है?
[EN] When used on booleans, && and || are short-circuiting—they skip evaluating the right operand if the left operand already determines the result. Single & and | always evaluate both sides on booleans and also double as bitwise operators on integers.
[HI] boolean पर && और || शॉर्ट-सर्किटिंग करते हैं अर्थात बाएँ पक्ष से उत्तर तय होने पर दायाँ पक्ष नहीं चलता। सिंगल & और | boolean में दोनों पक्ष चलाते हैं तथा पूर्णांकों पर बिटवाइज़ कार्य भी करते हैं।
Q2: Why does (n & 1) == 1 detect odd numbers better than (n % 2) == 1?
(n % 2) == 1 की तुलना में (n & 1) == 1 विषम संख्या की जांच बेहतर तरीके से क्यों करता है?
[EN] For negative odd numbers like -3, (-3 % 2) evaluates to -1 (not 1), which causes (n % 2 == 1) to falsely return false! Checking (n & 1) == 1 inspects only the lowest bit and works correctly for both positive and negative integers.
[HI] -3 जैसी ऋणात्मक विषम संख्या के लिए (-3 % 2) का मान -1 आता है, जिससे (n % 2 == 1) गलत रूप से false दे देता है। (n & 1) == 1 केवल सबसे निचला बिट जांचता है और सभी पूर्णांकों पर सटीक काम करता है।
Q3: Why is integer division by zero an exception while floating-point division by zero is not?
पूर्णांक में शून्य से विभाजन अपवाद देता है जबकि फ़्लोटिंग-पॉइंट में क्यों नहीं?
[EN] Integers have no bit pattern reserved for infinity or undefined values, so the JVM throws ArithmeticException. Floating-point follows the IEEE 754 standard, which defines bit patterns for +Infinity, -Infinity, and NaN.
[HI] पूर्णांकों में अनंत (Infinity) या अपरिभाषित मान के लिए कोई बिट पैटर्न नहीं होता, इसलिए JVM ArithmeticException फेंकता है; जबकि फ़्लोटिंग-पॉइंट IEEE 754 मानक के तहत Infinity और NaN दर्शाता है।
Post a Comment
Post a Comment