Оглавление:
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?
Оставить комментарий
Мой канал в социальных сетях
Полезные статьи:
Series: Go for Java Developers — analysis of trace, profiling and testing In this article we will analyze tools and practices for testing, debugging and profiling in Go. For a Java developer this wil…
Welcome to my IT blog, my name is Vitaly Lesnykh. I ve been in IT since 2004: I created my first website as a thesis in college, and I ve been developing apps and websites since then. I ve worked as …
Memory model is a layer between the program and the processor. Modern CPUs aggressively optimize execution: instructions may be reordered, data may be stored in core caches, and operations may be perf…
Новые статьи:
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. …