Java ProgramsBasics & I/ODemonstrate Bitwise OR

Demonstrate Bitwise OR in Java

beginner·  Basics & I/O  ·  Operators

Problem

The bitwise OR operator (|) compares two numbers bit by bit, producing a 1 in each position where either operand has a 1 there — it only produces a 0 where both operands have a 0.

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

Input
12, 10
Output
12 | 10 = 14

Java Program

Java
public class DemonstrateBitwiseOr { 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 = 14 (1110)

Core Logic

Lining up the binary form of both operands makes it clear that each result bit is 1 wherever either input bit is 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 = 1, position 1 is 0 | 1 = 1, position 0 is 0 | 0 = 0, giving 1110.
  4. 41110 in binary is 14 in decimal, which is what a | b evaluates to.
12 (1100) OR 10 (1010) keeps every bit position where either operand has a 1 — positions 3, 2, and 1 — giving 1110, or 14.
💡

Key Point: Bitwise OR is the standard way to set a specific bit without disturbing the others — n | (1 << k) turns on bit k of n while leaving every other bit exactly as it was.

Key Concepts

| operatorbitwise ORbinary representation

Approach 2: Java 8

Java
import java.util.function.IntBinaryOperator; public class BitwiseOrLambda { public static void main(String[] args) { int a = 12; int b = 10; // The | logic is stored as a named, reusable lambda IntBinaryOperator or = (x, y) -> x | y; int result = or.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 = 14 (1110)

Core Logic

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

How It Works
  1. 1IntBinaryOperator or = (x, y) -> x | y; stores the bitwise OR logic as a lambda.
  2. 2or.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, or.applyAsInt(12, 10) returns 14, same as a | b.
💡

Key Point: Naming the lambda or 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