← Back to Core Java Course | Chapter 14: Advanced Topics | Lesson 4 of 6

Java के एडवांस्ड Bitwise Operations

Advanced bitwise operations किसी number के individual 0s और 1s पर काम करते हैं, एक abacus पर beads move करने जैसा। Shifts उन्हें left या right slide करती हैं।

Bitwise AND and OR

Bitwise AND (&) हर output bit सिर्फ वहां सेट करता है जहां दोनों operands में 1 हो, और bitwise OR (|) हर output bit वहां सेट करता है जहां किसी भी operand में 1 हो — दोनों हर bit position पर independently operate करते हैं।

उदाहरण: Bitwise AND and OR

java
public class Main {
	public static void main(String[] args) {
		System.out.println(5 & 3); // AND: 1
		System.out.println(5 | 3); // OR: 7
	}
}

Bitwise XOR and Complement

Bitwise XOR (^) हर output bit वहां सेट करता है जहां exactly एक operand में 1 हो (लेकिन दोनों में नहीं), और bitwise complement (~) एक single value में हर bit flip करता है, हर 0 को 1 और इसका उल्टा बदलते हुए।

उदाहरण: Bitwise XOR and Complement

java
public class Main {
	public static void main(String[] args) {
		System.out.println(5 ^ 3); // XOR: 6
		System.out.println(~5); // complement: -6
	}
}

Left Shift Operator

Left shift (<<) सभी bits को higher end की ओर move करता है, खाली हुई positions को zeros से भरते हुए — यह shift किए गए हर position के लिए दो की power से multiply करने के बराबर है।

उदाहरण: Left Shift Operator

java
public class Main {
	public static void main(String[] args) {
		System.out.println(3 << 2); // 3 * 2^2 = 12
	}
}

Signed Right Shift Operator

Signed right shift (>>) bits को lower end की ओर move करता है जबकि sign bit संरक्षित रखता है, इसलिए एक negative number shift करना इसे negative रखता है — sign सही रखते हुए दो की power से divide करने के बराबर।

उदाहरण: Signed Right Shift Operator

java
public class Main {
	public static void main(String[] args) {
		System.out.println(-8 >> 1); // sign preserved: -4
	}
}

Unsigned Right Shift Operator

Unsigned right shift (>>>) sign की परवाह किए बिना हमेशा खाली हुई high-order bits को zero से भरता है, जो specifically तब मायने रखता है जब एक negative number के साथ काम करते हुए आपको sign-preserving behavior के बजाय एक positive result चाहिए।

उदाहरण: Unsigned Right Shift Operator

java
public class Main {
	public static void main(String[] args) {
		System.out.println(-8 >>> 1); // fills with zero, ignores sign
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. >> (sign रखता है) को >>> (zeros से भरता है) के साथ confuse करना।
  2. Type की bit width से ज़्यादा shift करना, जो shift count की सिर्फ low bits उपयोग करता है।
  3. && और || से अंतर जाने बिना booleans और integers पर & और | उपयोग करना।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.