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

Java Bitwise Operators

Bitwise operators work on the tiny 0s and 1s that make up numbers inside the computer, like flipping rows of light switches. They let you change individual switches.
Syntax
java
int result = a & b;  // also |, ^, ~
int result = a << n;  // also >>, >>>

Bitwise AND and OR

Bitwise & and | act on the individual binary bits of a number rather than its value as a whole: & keeps a bit only where both operands have a 1, while | sets a bit wherever either operand has a 1.

Example: 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) sets each output bit to 1 only when the two input bits differ -- a classic use is swapping two integers without a temporary variable, or toggling specific flag bits on and off.

Example: 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 ()

The bitwise complement operator ~ flips every single bit in a number's binary representation, which for signed integers is mathematically equivalent to computing -(x + 1).

Example: 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

<< shifts all bits left, filling with zeros on the right, which doubles the value for each position shifted; >> shifts bits right, which halves the value (rounding toward negative infinity) and is much faster than an actual division for powers of two.

Example: 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

Combining bitwise operations with assignment (&=, |=, ^=) lets you update flag-style variables in place, a common pattern when a single int is used to pack several true/false settings into individual bits.

Example: 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. #}
Common Mistakes
  1. Confusing & with &&, since & works on bits for integers and does not short-circuit for booleans.
  2. Expecting ~5 to be -5, when it is -6.
  3. Shifting by a large amount, which uses only the low 5 bits (for int), so 1 << 32 is 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.