Bitwise Operators in Java
×


Bitwise Operators in Java

119

Understanding Bitwise Operators in Java

Bitwise Operators in Java provide a way to manipulate individual bits of integer data types. Unlike arithmetic operators that work on whole numbers, bitwise operators work at the binary level, enabling direct control over bits. This makes them incredibly useful for tasks like setting flags, masking bits, or performing low-level programming.

Types of Bitwise Operators

Java offers several bitwise operators, each performing a unique operation on bits:

  • Bitwise AND (&): Compares bits of two numbers and returns 1 only if both bits are 1.
  • Bitwise OR (|): Returns 1 if at least one of the bits is 1.
  • Bitwise XOR (^): Returns 1 if bits are different, 0 if they are the same.
  • Bitwise NOT (~): Flips all bits, turning 0s into 1s and vice versa.
  • Left Shift (<<): Shifts bits to the left, adding zeros on the right.
  • Right Shift (>>): Shifts bits to the right, preserving the sign bit.
  • Unsigned Right Shift (>>>): Shifts bits to the right and fills leftmost bits with zeros.

Bitwise AND (&) Operator

The bitwise AND operator compares each bit of two numbers and results in a bit set to 1 only when both bits are 1.

Example of Bitwise AND


int a = 6;  // binary: 0000 0110
int b = 3;  // binary: 0000 0011
int result = a & b; // result: 0000 0010 (decimal 2)

System.out.println(result); // Output: 2

Bitwise OR (|) Operator

The OR operator returns a bit set to 1 if either or both bits in the compared position are 1.

Example of Bitwise OR


int a = 6;  // binary: 0000 0110
int b = 3;  // binary: 0000 0011
int result = a | b; // result: 0000 0111 (decimal 7)

System.out.println(result); // Output: 7

Bitwise XOR (^) Operator

XOR returns 1 when the bits being compared are different, and 0 when they are the same.

Example of Bitwise XOR


int a = 6;  // binary: 0000 0110
int b = 3;  // binary: 0000 0011
int result = a ^ b; // result: 0000 0101 (decimal 5)

System.out.println(result); // Output: 5

Bitwise NOT (~) Operator

The bitwise NOT operator flips every bit of the number, converting all 1s to 0s and all 0s to 1s. Because Java uses two's complement for negative numbers, the result appears as the negative of the number plus one.

Example of Bitwise NOT


int a = 6;          // binary: 0000 0110
int result = ~a;    // result: 1111 1001 (decimal -7)

System.out.println(result); // Output: -7

Bitwise Shift Operators

Bitwise shift operators move bits left or right in a binary number. This can be used to multiply or divide by powers of two quickly.

Left Shift (<<)

Shifts bits to the left and inserts zeros from the right.


int a = 5;       // binary: 0000 0101
int result = a << 2;  // result: 0001 0100 (decimal 20)

System.out.println(result); // Output: 20

Right Shift (>>)

Shifts bits to the right and preserves the sign bit (for negative numbers).


int a = 20;      // binary: 0001 0100
int result = a >> 2;  // result: 0000 0101 (decimal 5)

System.out.println(result); // Output: 5

Unsigned Right Shift (>>>)

Shifts bits right and fills with zeros regardless of the sign.


int a = -20;       // binary: 1111 1111 1111 1111 1111 1111 1110 1100 (two's complement)
int result = a >>> 2; // result: shifts bits right, fills left with 0s

System.out.println(result); // Output: 1073741819

Conclusion

Bitwise Operators in Java provide powerful tools for manipulating data at the bit level. Whether you're optimizing performance, working with flags, or handling low-level data, mastering these operators expands your programming toolkit and enhances your ability to work efficiently with binary data.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat