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:
Java was originally designed for multithreading and parallel computing. Over time, various methods for working with the results of asynchronous tasks have emerged—from the classic Future to modern Str...
In this article, we will examine the key aspects of memory management, runtime, and object allocation mechanisms in Go and Java. We will focus on the differences in approaches to memory management, wo...
Error handling in Go is significantly different from the familiar Java approach with exceptions. Instead of try/catch, Go uses returning an error as a separate value, and `defer` helps safely release ...
New Articles:
Concurrency is not about “starting many threads”. It’s about agreements between them. Imagine a restaurant kitchen: — cooks (threads / goroutines) — orders (tasks) — and the main question: how do th...
Imagine a typical production service. 32 CPU hundreds of threads configuration / session / rate limits cache tens of thousands of operations per second And somewhere inside — a regular Map. At first...
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. ...