0% found this document useful (0 votes)
8 views4 pages

Java Bitwise Operators Explained

Uploaded by

hemrajsaindane21
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Java Bitwise Operators Explained

Uploaded by

hemrajsaindane21
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Bitwise Operators in Java

• Speed: Bitwise operations are faster than arithmetic operations as they


operate directly on binary representations of numbers.
• Space Optimization: They can store multiple values in a single variable,
useful in memory-constrained environments.
• Bit Manipulation: They allow precise control over individual bits,
beneficial in applications like cryptography and error detection.
Bitwise operators in Java are used to perform operations on individual bits of
integer data types such as byte, short, int, and long. These operators are
essential for tasks that require direct manipulation of bits, such as low-level
programming, cryptography, and performance optimization.
There are 7 operators to perform bit-level operations in Java.
Operator Description
| Bitwise OR

& Bitwise AND

^ Bitwise XOR

~ Bitwise Complement

<< Left Shift

>> Signed Right Shift

>>> Unsigned Right Shift

Types of Bitwise Operators


Bitwise AND (&)
The bitwise AND operator returns 1 if both corresponding bits of the operands
are 1; otherwise, it returns 0.
a b a&b
0 0 0
0 1 0
1 0 0
1 1 1

int x = 9; // 1001 in binary


int y = 8; // 1000 in binary
[Link]("x & y = " + (x & y)); // Output: 8 (1000 in binary)

Bitwise OR (|)
The bitwise OR operator returns 1 if at least one of the corresponding bits of the
operands is 1; otherwise, it returns 0.
a b a|b
0 0 0
0 1 1
1 0 1
1 1 1

int x = 9; // 1001 in binary


int y = 8; // 1000 in binary
[Link]("x | y = " + (x | y)); // Output: 9 (1001 in binary)

Bitwise XOR (^)


The bitwise XOR operator returns 1 if the corresponding bits of the operands
are different; otherwise, it returns 0.
a b a^b
0 0 0
0 1 1
1 0 1
1 1 0

int x = 9; // 1001 in binary


int y = 8; // 1000 in binary
[Link]("x ^ y = " + (x ^ y)); // Output: 1 (0001 in binary)

Bitwise Complement (~)


The bitwise complement operator is a unary operator that inverts all the bits of
the operand, changing 0s to 1s and 1s to 0s.
int x = 2; // 0010 in binary
[Link]("~x = " + (~x)); // Output: -3 (1101 in binary)

Shift Operators
Left Shift (<<)
The left shift operator shifts the bits of the operand to the left by the specified
number of positions, filling the rightmost positions with 0s.

int x = 12; // 1100 in binary


[Link]("x << 1 = " + (x << 1)); // Output: 24 (11000 in binary)

Signed Right Shift (>>)


The signed right shift operator shifts the bits of the operand to the right by the
specified number of positions, filling the leftmost positions with the sign bit (0
for positive numbers, 1 for negative numbers).
int x = 50; // 110010 in binary
[Link]("x >> 2 = " + (x >> 2)); // Output: 12 (1100 in binary)

Unsigned Right Shift (>>>)


The unsigned right shift operator shifts the bits of the operand to the right by the
specified number of positions, filling the leftmost positions with 0s.
int x = 20; // 10100 in binary
[Link]("x >>> 2 = " + (x >>> 2)); // Output: 5 (101 in binary)
Example

public class BitwiseEx


{
public static void main(String[] args)
{
int a=20,b=40;
[Link]("Bitwise and-& :"+(a&b));
[Link]("Bitwise or-| :"+(a|b));
[Link]("Bitwise Xor-^ :"+(a^b));
int d=10,e=20,f=-20,g=20;
[Link]("left shift << :"+(d<<1));
[Link]("Right shift >> :"+(e>>1));
[Link]("Right shift with zero filled >>> :"+(g>>>1));
[Link]("Right shift with zero filled >>> :"+(f>>>1));
int x = 2; // 0010 in binary
[Link]("~x = " + (~x)); // Output: -3 (1101 in binary)
}
}

You might also like