Bitwise Operators in Java
Operators constitute the basic building block of any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they provide. Here are a few types:
- Arithmetic Operators
- Unary Operators
- Assignment Operator
- Relational Operators
- Logical Operators
- Ternary Operator
- Bitwise Operators
- Shift Operators
This article explains all that one needs to know regarding Bitwise Operators.
Bitwise operators are used to performing the manipulation of individual bits of a number. They can be used with any integral type (char, short, int, etc.). They are used when performing update and query operations of the Binary indexed trees.
Now let’s look at each one of the bitwise operators in Java:
1. Bitwise OR (|)
This operator is a binary operator, denoted by ‘|’. It returns bit by bit OR of input values, i.e., if either of the bits is 1, it gives 1, else it shows 0.
a = 5 = 0101 (In Binary) b = 7 = 0111 (In Binary) Bitwise OR Operation of 5 and 7 0101 | 0111 ________ 0111 = 7 (In decimal)
2. Bitwise AND (&)
This operator is a binary operator, denoted by ‘&.’ It returns bit by bit AND of input values, i.e., if both bits are 1, it gives 1, else it shows 0.
a = 5 = 0101 (In Binary) b = 7 = 0111 (In Binary) Bitwise AND Operation of 5 and 7 0101 & 0111 ________ 0101 = 5 (In decimal)
3. Bitwise XOR (^)
This operator is a binary operator, denoted by ‘^.’ It returns bit by bit XOR of input values, i.e., if corresponding bits are different, it gives 1, else it shows 0.
a = 5 = 0101 (In Binary) b = 7 = 0111 (In Binary) Bitwise XOR Operation of 5 and 7 0101 ^ 0111 ________ 0010 = 2 (In decimal)
4. Bitwise Complement (~)
This operator is a unary operator, denoted by ‘~.’ It returns the one’s complement representation of the input value, i.e., with all bits inverted, which means it makes every 0 to 1, and every 1 to 0.
a = 5 = 0101 (In Binary) Bitwise Complement Operation of 5 ~ 0101 ________ 1010 = 10 (In decimal)
Note: Compiler will give 2’s complement of that number, i.e., 2’s complement of 10 will be -6.
a&b = 5 a|b = 7 a^b = 2 ~a = -6 a= 5
a= 0011 b= 0110 a|b= 0111 a&b= 0010 a^b= 0101 ~a & b|a&~b= 0101 ~a= 1100
Bit-Shift Operators (Shift Operators)
Shift operators are used to shift the bits of a number left or right, thereby multiplying or dividing the number by two, respectively. They can be used when we have to multiply or divide a number by two.
number shift_op number_of_places_to_shift;
Types of Shift Operators:
Shift Operators are further divided into 4 types. These are:
- Signed Right shift operator (>>)
- Unsigned Right shift operator (>>>)
- Left shift operator(<<)
- Unsigned Left shift operator (<<<)
Note: For more detail about the Shift Operators in Java, refer Shift Operator in Java.
program to implement all Bitwise operators in java for user input
Enter first number: 4 Enter second number: 8
Bitwise AND: 0 Bitwise OR: 12 Bitwise XOR: 12 Bitwise NOT: -5 Bitwise Left Shift: 16 Bitwise Right Shift: 1 Bitwise Unsigned Right Shift: 1
This program prompts the user to enter two numbers, num1 and num2. It then performs the following bitwise operations using the &, |, ^, ~, <<, >>, and >>> operators:
Bitwise Left Shift
Bitwise Right Shift
Bitwise Zero Fill Right Shift
The advantages of using Bitwise Operators in Java are:
- Speed: Bitwise operations are much faster than arithmetic operations as they operate directly on binary representations of numbers.
- Space Optimization: Bitwise operations can be used to store multiple values in a single variable, which can be useful when working with limited memory.
- Bit Manipulation: Bitwise operators allow for precise control over individual bits of a number, which can be useful in various applications such as cryptography, error detection, and compression.
- Code Simplification: Bitwise operations can simplify the code by reducing the number of conditional statements and loops required to perform certain tasks.
- Improved Readability: Bitwise operations can make the code more readable by encapsulating complex logic into a single operation, making the code easier to understand and maintain.
In summary, Bitwise Operators are an important tool for optimizing performance, improving code readability, and reducing code complexity in Java applications.
Please Login to comment...