Java ProgramsBasics & I/ODemonstrate Bitwise AND

Demonstrate Bitwise AND in Java

beginner·  Basics & I/O  ·  Operators

Problem

The bitwise AND operator (&) compares two numbers bit by bit, producing a 1 in each position only where both operands have a 1 there — everywhere else it produces a 0.

Given two integers, compute their bitwise AND and show the binary representation of both operands and the result.

Input
12, 10
Output
12 & 10 = 8

Java Program

Java
public class DemonstrateBitwiseAnd { public static void main(String[] args) { int a = 12; int b = 10; int result = a & b; System.out.println("a = " + a + " (" + Integer.toBinaryString(a) + ")"); System.out.println("b = " + b + " (" + Integer.toBinaryString(b) + ")"); System.out.println("a & b = " + result + " (" + Integer.toBinaryString(result) + ")"); } }

Output

a = 12 (1100) b = 10 (1010) a & b = 8 (1000)

Core Logic

Lining up the binary form of both operands makes it clear that each result bit is 1 only where both input bits are 1.

How It Works
  1. 1Integer.toBinaryString(a) converts 12 into its 4-bit binary form, 1100.
  2. 2b = 10 is 1010 in binary.
  3. 3a & b compares the two bit by bit: position 3 is 1 & 1 = 1, position 2 is 1 & 0 = 0, position 1 is 0 & 1 = 0, position 0 is 0 & 0 = 0, giving 1000.
  4. 41000 in binary is 8 in decimal, which is what a & b evaluates to.
12 (1100) AND 10 (1010) keeps only the bit position where both have a 1 — position 3 — giving 1000, or 8.
💡

Key Point: Bitwise AND is the standard trick for checking or clearing specific bits — n & 1 tests whether n's lowest bit is set, which is exactly how n % 2 can be computed without division.

Key Concepts

& operatorbitwise ANDbinary representation

Approach 2: Java 8

Java
import java.util.function.IntBinaryOperator; public class BitwiseAndLambda { public static void main(String[] args) { int a = 12; int b = 10; // The & logic is stored as a named, reusable lambda IntBinaryOperator and = (x, y) -> x & y; int result = and.applyAsInt(a, b); System.out.println("a = " + a + " (" + Integer.toBinaryString(a) + ")"); System.out.println("b = " + b + " (" + Integer.toBinaryString(b) + ")"); System.out.println("a & b = " + result + " (" + Integer.toBinaryString(result) + ")"); } }

Output

a = 12 (1100) b = 10 (1010) a & b = 8 (1000)

Core Logic

Wrapping & in a named IntBinaryOperator turns 'AND these two ints' into a reusable value instead of a one-off inline expression.

How It Works
  1. 1IntBinaryOperator and = (x, y) -> x & y; stores the bitwise AND logic as a lambda.
  2. 2and.applyAsInt(a, b) calls it with a and b, returning the same result a & b would inline.
  3. 3The binary strings are built exactly as before, with Integer.toBinaryString().
With a = 12 and b = 10, and.applyAsInt(12, 10) returns 8, same as a & b.
💡

Key Point: Naming the lambda and makes it clear at the call site what operation it performs — the same pattern used for the arithmetic and relational operator pages.

Key Concepts

IntBinaryOperatorfunctional interfacelambda expression

Related Programs