Bitwise Logical Operator in Java

Bitwise logical operators are a set of operators that perform logical operations on individual bits. These operators are commonly used in computer programming to manipulate data stored in variables, particularly when working with low-level hardware or when optimizing code for efficiency. In this blog post, we’ll take a closer look at the four bitwise logical operators: AND (&), OR (|), XOR (^), and NOT (~).

Bitwise Logical AND (&)

The Bitwise logical AND operator compares each bit of its operands and returns 1 if both bits are 1, and 0 if either bit is 0. For example:

1010 & 1100 = 1000

In this example, the AND operator compares the two binary numbers 1010 and 1100, which are 10 and 12 in decimal, respectively. The resulting binary number 1000 is 8 in decimal.

Bitwise Logical OR (|)

The Bitwise logical OR operator compares each bit of its operands and returns 1 if either bit is 1, and 0 if both bits are 0. For example:

1010 | 1100 = 1110

In this example, the OR operator compares the two binary numbers 1010 and 1100, which are 10 and 12 in decimal, respectively. The resulting binary number 1110 is 14 in decimal.

Bitwise Logical XOR (^)

The Bitwise logical XOR operator compares each bit of its operands and returns 1 if the bits are different, and 0 if the bits are the same. For example:

1010 ^ 1100 = 0110

In this example, the XOR operator compares the two binary numbers 1010 and 1100, which are 10 and 12 in decimal, respectively. The resulting binary number 0110 is 6 in decimal.

Bitwise Logical NOT (~)

The NOT operator flips the value of each bit. For example:

~1010 = 0101

In this example, the Bitwise logical NOT operator flips the value of each bit in the binary number 1010, which is 10 in decimal. The resulting binary number 0101 is 5 in decimal.

It’s important to note that bitwise logical operators only work on integers, and they perform the operation on each individual bit rather than the entire number as a whole. This can be a powerful tool for manipulating data at a low level, but it can also be confusing if we’re not familiar with binary numbers.

Leave a Reply

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x