Bitwise operators in java with examples

Zora 131 Published: 07/06/2024

Bitwise operators in java with examples

I'm happy to help! Here's the explanation of bitwise operators in Java with examples:

Java provides several bitwise operators that can be used to manipulate binary data at the bit level. These operators are useful when working with flags, masks, or bit-level manipulation.

Here are some common bitwise operators:

Bitwise AND Operator ( & )

The bitwise AND operator returns 1 if both bits at the corresponding position in the two operands are 1; otherwise, it returns 0.

Example:

int a = 5; // binary: 00000101

int b = 3; // binary: 00000011

int result = a & b;

System.out.println(Integer.toBinaryString(result)); // prints: 00000001

Bitwise OR Operator ( | )

The bitwise OR operator returns 1 if at least one of the bits in the two operands is 1; otherwise, it returns 0.

Example:

int a = 5; // binary: 00000101

int b = 3; // binary: 00000011

int result = a | b;

System.out.println(Integer.toBinaryString(result)); // prints: 00000111

Bitwise XOR Operator ( ^ )

The bitwise XOR operator returns 1 if the corresponding bits in the two operands are different; otherwise, it returns 0.

Example:

int a = 5; // binary: 00000101

int b = 3; // binary: 00000011

int result = a ^ b;

System.out.println(Integer.toBinaryString(result)); // prints: 00000110

Bitwise NOT Operator ( ~ )

The bitwise NOT operator inverts all the bits of its operand. If a bit is currently 1, it becomes 0; if a bit is currently 0, it becomes 1.

Example:

int a = 5; // binary: 00000101

int result = ~a;

System.out.println(Integer.toBinaryString(result)); // prints: 11111010

Bitwise Left Shift Operator ( << )

The bitwise left shift operator shifts the bits of its operand to the left by the specified number of places. If a bit is shifted out of the left end, it is lost.

Example:

int a = 5; // binary: 00000101

int result = a << 1;

System.out.println(Integer.toBinaryString(result)); // prints: 00001010

Bitwise Right Shift Operator ( >> )

The bitwise right shift operator shifts the bits of its operand to the right by the specified number of places. If a bit is shifted out of the right end, it becomes 0.

Example:

int a = 5; // binary: 00000101

int result = a >> 1;

System.out.println(Integer.toBinaryString(result)); // prints: 00000010

These operators are useful in various situations, such as:

Setting or clearing flags Performing bit-level manipulation for compression, encryption, or other algorithms Creating masks to filter out unwanted bits

Remember to use the bitwise operators correctly and consider the implications of their usage on your program's logic.

What is the name of this '%' operator in java example

I'm happy to help you with that!

The operator % is commonly known as the modulus or remainder operator in Java, as well as in other programming languages like C, C++, Python, and many more.

So, what does it do?

Imagine you have a dividend (let's say 17) and a divisor (let's say 5). When you divide the dividend by the divisor, you get a quotient (2) and a remainder (2).

The modulus operator % gives you that remainder. In our example:

int remainder = 17 % 5;

In this case, remainder would be equal to 2.

Here are some key takeaways about the modulus operator:

Remainder calculation: As mentioned earlier, the modulus operator calculates the remainder of an integer division. Divisor is not zero: You can't use the modulus operator with a divisor of zero (or null), as that would cause a DivisionByZeroException or similar error. Multiple uses: Modulus operators are often used in algorithms like modular arithmetic, checksum calculations, and hash functions.

When to use it?

Check if a number is odd or even: By taking the modulus of the number with 2 (e.g., x % 2), you can determine whether the number is even (remainder 0) or odd (remainder 1). Calculate dates and times: Modulus operations are essential in date calculations, such as finding the day of the week, calculating the remainder of days in a month, or determining the time elapsed between two dates. Encryption and decryption: The modulus operator plays a crucial role in many encryption algorithms like RSA (Rivest-Shamir-Adleman) and others.

In conclusion, the % operator is a fundamental part of Java and other programming languages, allowing you to calculate remainders and perform various arithmetic operations.

Would you like me to explain more about its usage or provide examples?