Java provides an extensive bit manipulation operator for programmers who want to communicate directly with the hardware. These operators are used for testing, setting or shifting individual bits in a value. In order to work with these operators, one should be aware of the binary numbers and two's complement format used to represent negative integers.
The bitwise operators can be used on values of type long, int, short, char or byte, but cannot be used with boolean, float, double, array or object type.
The bitwise AND (&) bitwise OR (!) and bitwise Exclusive OR (^)are the three logical bitwise operators. These are the binary operators which compare the two operands bit by bit. If either of the operands with a bitwise operator is of type long then the result will be long, otherwise the result is of type int.
Bitwise AND (&)operator combines its two integer operands by performing a logical AND operation on their individual bits. It sets each bit in the result to 1 if corresponding bits in both operands are 1. One of the applications of bitwise AND operator is forcing selected bits of an operand to 0.
Bitwise OR (|)operator combines its two integer operands by performing a logical OR operation on their individual bits. It sets each bit in the result to 1if the corresponding bits in either or both of the operands are 1. One of the applications of bitwise OR is forcing selected bits of an operand to 1.
Bitwise Exclusive OR operator (^) combines its two integer operands by performing a logical XOR operands on their individual bits. It sets each bit in the result to 1 if the corresponding bits in two operands are different. One of the applications of bitwise Exclusive OR operator is to change 0's to 1's and l's to 0's.
Bitwise One's Complement operator (-) Bitwise complement operator (-) is a unary operator. It inverts each bit of its operand i.e. Is become Os and Os become Is. This operator can be used to
1. To encrypt the contents of a file which can later be decrypted.
2. To store negative numbers in some computers that supports one's complement method for storing negative number.
Let us consider an operand a = 0000 0000 0001 0010 then on performing (~a) we get
-a = 1111 1111 1110 1101
Bitwise shift left operator (<<) shifts the bits of the left operand to left by number of positions specified by the right operand. The high order bits of the left operand are lost and 0 bits are shifted in from the right .It has the following syntax
Operand1 << operand2
Here, operand1 is binary representation of a number to be the shifted and operand2 represents the number of positions by which it is shifted.
Bitwise Shift Right operator (>>)shifts the bits of the left operand to right by a number of positions specified by the right operand. The low order bits of the left operand are lost and the high order bits shifted in are either 0 or 1 depending upon whether the left operand is positive or negative. If the left operand is positive, Os are shifted into the high order bits and if the left operand is negative, 1's are shifted instead. It has the following syntax.
operand1>>operand2
Here, operand1 is the binary representation of a number to be shifted and operand2 represents the number of positions by which it is shifted.
Bitwise Shift Right with zero fill (>>>) operator is similar to bitwise shift right (>>) operator with the exception that it always shifts zero's into the high order bits of the result regardless of the sign of the left hand operand.
//program Showing bitwise Operators
import static java.lang.Long .*;
public class BitwiseOperators
{
public static void main (String [] args)
{
Short x=20,y=0xaf ;
Short z= -24;
System.out.println(" x & y --> " + (x & y));
System.out.println(" x | y --> " + (x | y));
System.out.println(" x ^ y --> " + (x ^ y));
System.out.println(" z << 2 --> " + (z<<2));
System.out.println(" z >>> 2 --> " + (z>>>2));
System.out.println(" z >> 2 --> " + (z>>2));
}
}
Advance Subjects
Basic Subjects
Social Us