- Java Programming Basics
- Java Tutorial
- Java Environment Setup
- Java Separators
- Java Data Types
- Java Variables
- Java Variable Scope
- Java Type Casting
- Java Operators
- Java Increment Decrement
- Java Left Shift
- Java Right Shift
- Java Bitwise Operators
- Java Ternary Operator
- Java Control Statements
- Java if-else statement
- Java for Loop
- Java while Loop
- Java do-while Loop
- Java switch Statement
- Java break Statement
- Java continue Statement
- Java Popular Topics
- Java Arrays
- Java Multidimensional Array
- Java Strings
- Java Methods
- Java Date and Time
- Java Exception Handling
- Java File Handling
- Java OOP
- Java Classes and Objects
- Java Constructors
- Java Constructor Overloading
- Java Object as Parameter
- Java Returning Objects
- Java Encapsulation
- Java Abstraction
- Java Inheritance
- Java Polymorphism
- Java Packages
- Java Import Statement
- Java Multithreading
- Java Suspend Resume Stop Thread
- Java Programming Examples
- Java Programming Examples
Java Bitwise Operators
Java bitwise operators manipulate the bits of binary numbers. Java supports the following six bitwise operators:
- Bitwise AND (&): carries out a bitwise AND operation on the corresponding bits of two integers. If both bits are 1, the result is 1, otherwise it is 0.
- Bitwise OR (|): carries out a bitwise OR operation on the corresponding bits of two integers. If at least one bit is 1, the result is 1, otherwise it is 0.
- Bitwise XOR (^): carries out a bitwise exclusive OR (XOR) operation on the corresponding bits of two integers. If the bits are different, the result is 1, otherwise it is 0.
- Bitwise NOT (˜): On an integer, it performs a bitwise complement operation. This operator flips all of the integer's bits, converting all 0s to 1s and all 1s to 0s.
- Bitwise Left Shift (<<): shifts the bits of an integer by a specified number of positions to the left. The 0s fill the empty positions on the right. This is equivalent to multiplying the integer by two multiplied by the shift amount raised to the power of two.
- Bitwise Right Shift (>>): shifts the bits of an integer by a specified number of positions to the right. The empty left-hand positions are filled with 0s for non-negative numbers and 1s for negative numbers (to preserve the sign of the number). This is the same as multiplying the integer by two raised to the power of the shift amount.
The "left shift" and "right shift" have already been discussed in the previous two posts.
This table shows the outcome of each operation. In the discussion that follows, always keep in mind that the bitwise operators are applied to each individual bit within each operand.
A | B | A | B | A & B | A ^ B | ~A |
---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 1 |
1 | 0 | 1 | 0 | 1 | 0 |
0 | 1 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 | 0 |
The bitwise AND operator in Java
The bitwise AND operator, &, produces a 1 bit if both the operands are also 1. A zero is produced in all the other cases.
Following is an example that demonstrates the bitwise AND operator:
0010101042 & 0000111115 ___________ 0000101010
The bitwise OR operator in Java
The bitwise OR operator, |, combines bits so that if either of the bits in the operands is 1, then the result bit is also 1.
Following is an example that demonstrates the bitwise OR operator:
0010101042 | 0000111115 ___________ 0010111147
The bitwise NOT operator in Java
The bitwise NOT operator, also called the bitwise complement, the unary NOT operator (˜), inverts all of the bits of its operand.
For example, the number 42 has this bit pattern:
00101010
becomes
11010101
after the NOT operator is applied.
The bitwise XOR operator in Java
The bitwise XOR operator, ^, combines bits such that if exactly one operand is 1, then the result is 1. Otherwise, the result is zero.
The following example shows the effect of the bitwise XOR (^) operator. This example also demonstrates a useful attribute of a XOR operation. Notice here how the bit pattern of 42 is inverted wherever the second operand has a 1 bit. Wherever the second operand has 0 bit, the first operand is unchanged. You will find this property useful when performing some types of bit manipulations.
0010101042 ^ 0000111115 _________ 0010010137
The Bitwise Logical Operators Example
The following program demonstrates the bitwise logical operators:
public class BitwiseOperatorsExample { public static void main(String[] args) { int a = 0b10101010; int b = 0b01010101; int resultAnd = a & b; System.out.println("a & b = " + Integer.toBinaryString(resultAnd)); int resultOr = a | b; System.out.println("a | b = " + Integer.toBinaryString(resultOr)); int resultXor = a ^ b; System.out.println("a ^ b = " + Integer.toBinaryString(resultXor)); int resultNotA = ˜a; System.out.println("˜a = " + Integer.toBinaryString(resultNotA)); int resultLeftShift = a << 2; System.out.println("a << 2 = " + Integer.toBinaryString(resultLeftShift)); int resultRightShift = a >> 2; System.out.println("a >> 2 = " + Integer.toBinaryString(resultRightShift)); } }
a & b = 0 a | b = 11111111 a ^ b = 11111111 ˜a = 1010101 a << 2 = 1010101000 a >> 2 = 101010
This (aforementioned) Java code shows how to use bitwise operators to perform various operations on two integer variables, a and b, which represent their binary values of 0b101010 (170 in decimal) and 0b01010101 (85 in decimal), respectively.
The following is another example program in Java that demonstrates the bitwise operators.
public class JavaProgram { private static final String[] BINARY_VALUES = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1110", "1111" }; public static void main(String[] args) { int binaryA = 0b0011; int binaryB = 0b0110; int bitwiseOr = binaryA | binaryB; int bitwiseAnd = binaryA & binaryB; int bitwiseXor = binaryA ^ binaryB; int complexExpression = (˜binaryA & binaryB) | (binaryA & ˜binaryB); int bitwiseNot = ˜binaryA & 0x0f; System.out.println("binaryA = " + BINARY_VALUES[binaryA]); System.out.println("binaryB = " + BINARY_VALUES[binaryB]); System.out.println("binaryA | binaryB = " + BINARY_VALUES[bitwiseOr]); System.out.println("binaryA & binaryB = " + BINARY_VALUES[bitwiseAnd]); System.out.println("binaryA ^ binaryB = " + BINARY_VALUES[bitwiseXor]); System.out.println("(˜binaryA & binaryB) | (binaryA & ˜binaryB) = " + BINARY_VALUES[complexExpression]); System.out.println("˜binaryA = " + BINARY_VALUES[bitwiseNot]); } }
binaryA = 0011 binaryB = 0110 binaryA | binaryB = 0111 binaryA & binaryB = 0010 binaryA ^ binaryB = 0101 (˜binaryA & binaryB) | (binaryA & ˜binaryB) = 0101 ˜binaryA = 1100
« Previous Tutorial Next Tutorial »