Friday, January 10, 2025
HomeProgrammingBitwise operators in Java

Bitwise operators in Java

Bitwise operators in Java are used to perform operations at the bit level. They manipulate individual bits of integers, making them useful for low-level programming tasks like encoding, encryption, and bit manipulation.

Bitwise Operators:
1. AND (&): Sets each bit to 1 if both bits are 1.
java
int a = 5; // 0101
int b = 3; // 0011
int result = a & b; // 0001 (1 in decimal)

See also  Difference Between Abstract Class and Interface in Java

2. OR (|): Sets each bit to 1 if at least one of the bits is 1.
java
int result = a | b; // 0111 (7 in decimal)

3. XOR (^): Sets each bit to 1 if only one of the bits is 1.
java
int result = a ^ b; // 0110 (6 in decimal)

See also  Python String Format () Method

4. Complement (~): Inverts all the bits.
java
int result = ~a; // 1010 (in 4-bit representation, -6 in decimal)

5. Shift Operators:
– Left Shift (<<): Shifts bits to the left, filling with zeros.
– Right Shift (>>): Shifts bits to the right, preserving the sign bit.
– Unsigned Right Shift (>>>): Shifts bits to the right, filling with zeros.

See also  How to Drop a Table If It Exists in SQL Server

Bitwise operators enhance performance in tasks that require direct bit manipulation

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x