Bitwise Operators in Java

Bitwise Operators in Java

In the Java programming language, several bitwise operators are defined. These operators are applied to integer data types, such as byte, short, int, long, and char.

List of Bitwise Operators

  • & — bitwise AND (AND)
  • | — bitwise OR (OR)
  • ^ — bitwise XOR (XOR)
  • ~ — bitwise NOT (NOT)
  • << — left shift
  • >> — right shift
  • >>> — right shift with zero fill
Bitwise operators perform operations on each bit of the numbers individually.

Example

 int a = 55; 
int b = 24; 
System.out.println(Integer.toBinaryString(a)); // 110111 
System.out.println(Integer.toBinaryString(b)); // 11000 

After conversion to binary code:

 a = 110111 b = 011000 

Bitwise AND (&)

The operator & (AND) returns 1 in the bit where both operands have the bit 1. Otherwise, it returns 0.

Example:

 int result = a & b; 
System.out.println(Integer.toBinaryString(result)); // 10000 
System.out.println(result); // 16 

Step-by-step explanation:

Operand Bits
a = 55 110111
b = 24 011000
a & b 010000

The result is the binary number 010000, which is equal to 16 in decimal.

Bitwise OR (|)

The operator | returns 1 in the bit position where at least one of the operands has the bit 1.

 int result = a | b; 
System.out.println(Integer.toBinaryString(result)); // 111111 System.out.println(result); // 63 
Operand Bits
a = 55 110111
b = 24 011000
a | b 111111

Result: 111111 = 63.

Bitwise Exclusive OR (^)

The operator ^ (XOR) returns 1 in the bit position where the bits differ, and 0 where they are the same.

 int result = a ^ b; 
System.out.println(Integer.toBinaryString(result)); // 101111 System.out.println(result); // 47 
Operand Bits
a = 55 110111
b = 24 011000
a ^ b 101111

Result: 101111 = 47.

Bitwise NOT (~)

The operator ~ inverts all bits of a number — all 1 become 0, and vice versa.

 int a = 55; 
System.out.println(Integer.toBinaryString(~a)); // ...the result will be in two's complement System.out.println(~a); // -56 
In bitwise negation, Java uses the representation of numbers in two's complement, so the result is negative.

Bitwise Shifts

  • << — shifts bits to the left, filling with zeros on the right (multiplication by 2 to the power of N)
  • >> — shifts bits to the right, preserving the sign (division by 2 to the power of N)
  • >>> — shifts to the right, filling with zeros (regardless of the sign)

Example:

 int a = 8; 
System.out.println(a << 2); // 32 (8 * 2^2) 
System.out.println(a >> 1); // 4 (8 / 2^1) 
System.out.println(a >>> 1); // 4 (the same result for positive numbers) 

Homework

  1. Create two variables int a and int b with different values.
  2. Output their binary representation using Integer.toBinaryString().
  3. Apply all bitwise operators to them: &, |, ^, ~, <<, >>, >>>.
  4. For each result, display both binary and decimal forms.
  5. Try to explain why you obtained such results.

Test — How well did you understand the lesson?


🌐 На русском
Total Likes:0

Оставить комментарий

My social media channel
By sending an email, you agree to the terms of the privacy policy

Useful Articles:

Pointers, functions, and execution control in Go vs Java | Types - Language
Series: Go for Java Developers — analyzing pointer, closures, defer, panic/recover In this article, we will analyze how Go manages the state and lifecycle of functions. A feature of Go is the ease of...
Internal structure of the Garbage Collector: Go ↔ Java
In this article, we will thoroughly examine the work of the garbage collector (Garbage Collector, GC) in Go and Java, discuss key internal mechanisms: concurrent mark &amp; sweep, mutator vs collector...
Breaking down: Rate-limiter, non-blocking operations, scheduler Go vs Java | Concurrency part 4
This article is dedicated to understanding the principles of working with concurrency and synchronization in Go and Java. We will look at key approaches such as rate-limiter, non-blocking operations, ...

New Articles:

Zero Allocation in Java: what it is and why it matters
Zero Allocation — is an approach to writing code in which no unnecessary objects are created in heap memory during runtime. The main idea: fewer objects → less GC → higher stability and performance. ...
Stream vs For in Java: how to write the fastest code possible
In Java, performance is often determined not by the "beauty of the code," but by how it interacts with memory, the JIT compiler, and CPU cache. Let s analyze why the usual for is often faster than Str...
Compiler, Build, and Tooling in Go and Java: how assembly, initialization, analysis, and diagnostics are organized in two ecosystems
This article is dedicated to a general overview of how the compiler, build, and tooling practices are arranged in Go, and how to better understand them through comparison with Java. We will not delve ...
Fullscreen image