← Back to Core Java Course | Chapter 3: Operators | Lesson 5 of 9

Java के Bitwise Operators

Bitwise operators उन छोटे 0s और 1s पर काम करते हैं जो computer के अंदर numbers बनाते हैं, light switches की rows flip करने जैसा। वे आपको individual switches बदलने देते हैं।
Syntax
java
int result = a & b;  // also |, ^, ~
int result = a << n;  // also >>, >>>

Bitwise AND and OR

Bitwise & और | किसी number की individual binary bits पर act करते हैं बजाय इसकी पूरी value के: & सिर्फ वहां एक bit रखता है जहां दोनों operands में 1 हो, जबकि | जहां भी किसी operand में 1 हो वहां एक bit सेट करता है।

उदाहरण: Bitwise AND and OR

java
public class Main {
	public static void main(String[] args) {
		int a = 6;  // 110
		int b = 3;  // 011
		System.out.println(a & b); // 2 - 1 only where both have a 1
		System.out.println(a | b); // 7 - 1 where either has a 1
	}
}

Bitwise XOR (^)

^ (XOR) हर output bit को सिर्फ 1 सेट करता है जब दो input bits अलग हों -- एक classic उपयोग बिना temporary variable के दो integers swap करना है, या specific flag bits को on और off toggle करना।

उदाहरण: Bitwise XOR (^)

java
public class Main {
	public static void main(String[] args) {
		int a = 5, b = 3;
		System.out.println(a ^ b); // 1 - set where bits differ
		a ^= b; b ^= a; a ^= b; // classic swap without a temp variable
		System.out.println(a + " " + b);
	}
}

Bitwise Complement ()

Bitwise complement operator ~ किसी number के binary representation में हर single bit flip करता है, जो signed integers के लिए mathematically -(x + 1) compute करने के बराबर है।

उदाहरण: Bitwise Complement ()

java
public class Main {
	public static void main(String[] args) {
		int x = 5;
		System.out.println(~x); // -6, equivalent to -(x + 1)
	}
}

Shift Operators

<< सभी bits left shift करता है, right पर zeros से भरते हुए, जो shift किए गए हर position के लिए value double करता है; >> bits right shift करता है, जो value आधा करता है (negative infinity की ओर rounding करते हुए) और दो की powers के लिए एक असली division से कहीं तेज़ है।

उदाहरण: Shift Operators

java
public class Main {
	public static void main(String[] args) {
		int x = 4;
		System.out.println(x << 1); // 8 - doubles the value
		System.out.println(x >> 1); // 2 - halves the value
	}
}

Bitwise Compound Assignment

Bitwise operations को assignment (&=, |=, ^=) के साथ combine करना आपको flag-style variables को in place update करने देता है, एक आम pattern जब एक single int कई true/false settings को individual bits में pack करने के लिए उपयोग होता है।

उदाहरण: Bitwise Compound Assignment

java
public class Main {
	public static void main(String[] args) {
		int flags = 0b0100;
		flags |= 0b0001; // sets a bit in place
		flags &= 0b0111; // clears a bit in place
		System.out.println(flags);
	}
}
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. & को && के साथ confuse करना, क्योंकि & integers के लिए bits पर काम करता है और booleans के लिए short-circuit नहीं करता।
  2. यह उम्मीद करना कि ~5 -5 होगा, जब यह -6 है।
  3. एक बड़ी मात्रा में shift करना, जो सिर्फ low 5 bits उपयोग करता है (int के लिए), इसलिए 1 << 32 1 है।

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.