java Bitwise Operators

Java Bitwise Operators

Bitwise operators in the Java programming language are used to perform operations such as shifting, modifying, and transferring numeric binary data at the bit level within computer storage locations. Bitwise operators perform bit-shifting operations on integer data types such as byte, short, int, and long. They allow programmers to directly manipulate multiple bit types to represent numeric values ​​as binary values. Bitwise operators help Java programmers with low-level bit-numeric data processing or optimize the performance of program algorithms.

java Bitwise Operators

Bitwise Operators in the Java Programming.

  • Bitwise AND (&)
  • Bitwise OR (|)
  • Bitwise XOR (^)
  • Bitwise Complement (~)
  • Left Shift (<<)
  • Right Shift (>>)
  • Unsigned Right Shift (>>>)

Bitwise AND (&) Operator in Java Programming.

In Java programs, the bitwise AND operator (&) helps perform a logical AND operation on a common bit of two operands. The output result is 1 if both bits are 1; otherwise, it displays 0.

Example of the Bitwise AND (&) Operator.

public class Main

{

public static void main(String[] args) {

int p = 15; // Equivalent binary value is – 1111

int q = 9; // Equivalent binary value is – 1001

int output = p & q; // Binary value output – 1001 -> Decimal – 9

System.out.println(“Output of p & q And operator – ” + output); // Result is – 9

}

}

Bitwise AND (&) Operator Explanation.

  • Here, the binary equivalent of 15 decimals is 1111.
  • Here, the binary equivalent of 9 decimals is 1001.
  • The & operator outputs 1001 as its binary equivalent.

Bitwise OR (|) Operator in Java Programming.

In Java programming, the bitwise OR operator (|) helps perform a logical OR data type operation on the corresponding value bits of two operands. Here, the output result is 1 if at least one bit is 1, otherwise, it is 0.

Example of the Bitwise OR (|) Operator.

public class Main

{

public static void main(String[] args) {

int p = 15; // Equivalent binary value is – 1111

int q = 9; // Equivalent binary value is – 1001

int output = p | q; // Binary value output – 1111 -> Decimal – 15

System.out.println(“Output of p | q Or operator – ” + output); // Result is – 15

}

}

Bitwise OR (|) Operator Explanation.

  • Here, the binary equivalent of 15 decimal is 1111.
  • Here, the binary equivalent of 9 decimal is 1001.
  • The | operator outputs the binary equivalent of 1111 as a result.

Bitwise XOR (^) Operator in Java Programming.

The bitwise XOR exclusive OR operator (^) in Java programs helps perform a logical XOR operation on the corresponding bits of two program operand values. Here, if both the bits of the ^XOR exclusive OR are different values, then the result is 1; otherwise, the result is 0.

Example of the Bitwise XOR (^) operator.

public class Main

{

public static void main(String[] args) {

int p = 15; // Equivalent binary value is – 1111

int q = 9; // Equivalent binary value is – 1001

int output = p ^ q; // Xor binary value output – 0110 -> Decimal – 6

System.out.println(“Output of p ^ q XOr operator – ” + output); // Result is – 6

}

}

Bitwise XOR (^) operator explanation.

  • Here, the binary equivalent of 15 decimal is 1111.
  • Here, the binary equivalent of 9 decimals is 1001.
  • The ^ operator outputs 1111, which is the binary equivalent of 6.

Bitwise Complement (~) Operator in Java Programming.

The bitwise complement operator (~) is used in Java programming to invert or reverse the value of each bit of a bitwise operand. This means that binary values ​​0 are converted to 1, and ones are converted to 1. Bitwise complement is also known as one’s complement.

Element of the Bitwise Complement (~) Operator.

  • Here, the unary operator acts on a single operand at a time.
  • It negates the value, returning the two’s complement representation as the result.

Example of the Bitwise Complement (~) Operator.

public class Main

{

public static void main(String[] args) {

int p=15; // Equivalent Binary values ​​- 0000 0000 0000 0000 0000 0000 0000 1111 (32-bit binary value representation)

int output = ~p; // Binary Complement output value – 1111 1111 1111 1111 1111 1111 1111 0000 -> Decimal -16

System.out.println(“The output is Bitwise Complement (~) of ~p – ” + output); // Result – 16

}

}

Bitwise Complement (~) Operator Explanation.

  • Here, the two’s complement representation (in 32-bit) of the bitwise complement operator 15 is 0000 0000 0000 0000 0000 0000 0000 1111.
  • Applying the complement (~) here reverses the values ​​of all the bits, creating 1111 1111 1111 1111 1111 1111 0000, which represents the two’s complement value of 16.

Left Shift (<<) Operator in Java Programming.

In Java programming, the left shift operator (<<) shifts or moves the bit values ​​of a number to the left by a fixed numeric value. Here, each left shift operation in the bit shifting process is equivalent to multiplying the number by 2, where zeros are shifted to the rightmost bits.

Example of the Left Shift (<<) Operator.

public class Main

{

public static void main(String[] args) {

int p = 15; // Here the decimal binary equivalent value is – 0000 0000 0000 0000 0000 0000 0000 1111 (32-bit decimal to binary representation)

int result = p << 2; // Here left shifts bits 2 places to the left and is – 60 (Binary – 0000 0000 0000 0000 0000 0000 0011 1100)

System.out.println(“The output of p left shift operation is << 2 – ” + result); // Result is – 60

}

}

Explanation of the Left Shift (<<) operator.

  • Here 15 is the binary conversion of the decimal value 00000000 00000000 00000000 00001100 to a binary 32-bit value.
  • Here, shifting the binary value of 15 decimal by 2 positions to the left results in the binary value 0000 0000 0000 0000 0000 0000 0011 1100, which is a binary value of 60.

As a result.

  • 15 * 2^2 = 15 * 4 = 60

Right Shift (>>) Operator in Java Programming.

In Java programs, the right shift operator (>>) shifts the bit value of a number by a fixed numeric position to the right. Here, each right shift operation is equivalent to dividing the number by 2, with rounding towards negative infinity for negative numeric values. Where the leftmost bits in the right shift operation are filled with the sign bit, this process is known as an arithmetic shift.

Example of the Right Shift (>>) Operator.

public class Main

{

public static void main(String[] args) {

int p=15; // here decimal Binary equivalent value is – 0000 0000 0000 0000 0000 0000 0000 1111 (32-bit decimal to binary representation)

int result = p >> 2; // here right Shifts bits 2 places to the right is -> 3 (Binary – 0000 0000 0000 0000 0000 0000 0011 1100)

System.out.println(“The Output of p right shift operation is >> 2 – ” + result); // Result is – 3

}

}

Right Shift (>>) Operator Explanation.

  • Here, the 32-bit binary value 00000000 000000000 00000000 00001100 is the decimal equivalent of 15.
  • Here, shifting this value by 2 positions to the right gives 0000 0000 0000 0000 0000 0000 0011 1100, the binary equivalent of 3.

As a result.

  • 15 / 2^2 = 15 / 4 = 3

Unsigned Right Shift (>>>) Operator in Java Programming.

The unsigned right shift operator (>>>) in Java programs shifts or moves the data value to the right, but it fills the leftmost bits with zeros during the bit shifting process. The unsigned right shift operator is used with unsigned integer data values ​​and is commonly applied to handle bitwise operations on those values, a process known as unsigned value shifting.

Detail explanation of Bitwise Operators

OperatorBitwise DescriptionBitwise Operator Example
&Bitwise AND operator used to Sets each bit to 1 if both bits are in 1 naturep & q
|Bitwise OR operator used to Sets each bit to 1 if both bits are in 1 naturep | q
^Bitwise XOR operator used to Sets each bit to 1 if only one of the bits is 1 in naturep ^ q
~Bitwise complement operator Inverts all the bits value.~p
<< Left shift operator used to Shifts bits to the left by a specified number of positions given by user.p << n
>> Right shift operator used to Shifts bits to the right, filling with the sign bit value.p >> n
>>> Unsigned right shift operator used to Shifts bits to the right, filling with 0s values.p >>> n

Unsigned Right Shift (>>>) Operator Example.

public class Main

{

public static void main(String[] args) {

int p = -15; // Here the decimal binary equivalent value is – 1111 1111 1111 1111 1111 1111 0001 (32-bit decimal to binary representation)

int result = p >>> 2; // Here the unsigned right shift bits 2 places and fill the value with zeros

System.out.println(“The output of p’s Unsigned Right Shift operation is >>> 2 – ” + result); // Result is – 1073741820

}

}

Unsigned Right Shift (>>>) Operator Explanation.

  • Here, in the case of a negative number value, the unsigned right shift operator fills the leftmost bits with a value of 0 instead of the sign bit (1).
  • The result is a positive number value, which is slightly different from the result of a regular right shift operation (>>).

Leave a Reply