Table of Contents:
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
- Create two variables
int aandint bwith different values. - Output their binary representation using
Integer.toBinaryString(). - Apply all bitwise operators to them:
&,|,^,~,<<,>>,>>>. - For each result, display both binary and decimal forms.
- Try to explain why you obtained such results.
Test — How well did you understand the lesson?
Оставить комментарий
My social media channel
Useful Articles:
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...
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 & sweep, mutator vs collector...
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 — 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. ...
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...
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 ...