Java के एडवांस्ड Bitwise Operations
In this page:
Bitwise AND and OR
Bitwise AND (&) हर output bit सिर्फ वहां सेट करता है जहां दोनों operands में 1 हो, और bitwise OR (|) हर output bit वहां सेट करता है जहां किसी भी operand में 1 हो — दोनों हर bit position पर independently operate करते हैं।
उदाहरण: Bitwise AND and OR
public class Main {
public static void main(String[] args) {
System.out.println(5 & 3); // AND: 1
System.out.println(5 | 3); // OR: 7
}
}
Login to try C/C++/Java/PHP code in the editor
Bitwise XOR and Complement
Bitwise XOR (^) हर output bit वहां सेट करता है जहां exactly एक operand में 1 हो (लेकिन दोनों में नहीं), और bitwise complement (~) एक single value में हर bit flip करता है, हर 0 को 1 और इसका उल्टा बदलते हुए।
उदाहरण: Bitwise XOR and Complement
public class Main {
public static void main(String[] args) {
System.out.println(5 ^ 3); // XOR: 6
System.out.println(~5); // complement: -6
}
}
Login to try C/C++/Java/PHP code in the editor
Left Shift Operator
Left shift (<<) सभी bits को higher end की ओर move करता है, खाली हुई positions को zeros से भरते हुए — यह shift किए गए हर position के लिए दो की power से multiply करने के बराबर है।
उदाहरण: Left Shift Operator
public class Main {
public static void main(String[] args) {
System.out.println(3 << 2); // 3 * 2^2 = 12
}
}
Login to try C/C++/Java/PHP code in the editor
Signed Right Shift Operator
Signed right shift (>>) bits को lower end की ओर move करता है जबकि sign bit संरक्षित रखता है, इसलिए एक negative number shift करना इसे negative रखता है — sign सही रखते हुए दो की power से divide करने के बराबर।
उदाहरण: Signed Right Shift Operator
public class Main {
public static void main(String[] args) {
System.out.println(-8 >> 1); // sign preserved: -4
}
}
Login to try C/C++/Java/PHP code in the editor
Unsigned Right Shift Operator
Unsigned right shift (>>>) sign की परवाह किए बिना हमेशा खाली हुई high-order bits को zero से भरता है, जो specifically तब मायने रखता है जब एक negative number के साथ काम करते हुए आपको sign-preserving behavior के बजाय एक positive result चाहिए।
उदाहरण: Unsigned Right Shift Operator
public class Main {
public static void main(String[] args) {
System.out.println(-8 >>> 1); // fills with zero, ignores sign
}
}
Login to try C/C++/Java/PHP code in the editor
>>(sign रखता है) को>>>(zeros से भरता है) के साथ confuse करना।- Type की bit width से ज़्यादा shift करना, जो shift count की सिर्फ low bits उपयोग करता है।
&&और||से अंतर जाने बिना booleans और integers पर&और|उपयोग करना।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: