Home Java Jdk7 Bitwise Operators in java 7
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Bitwise Operators in java 7

Advertisement
In this section you will learn about the Bitwise Operators. This is one type of operators.

Bitwise Operators in java 7

In this section you will learn about the Bitwise Operators. This is one type of operators.

Bitwise Operators :

Bitwise and Bit Shift Operators : This is another kind of java programming operators and these are used for bitwise and bit shift operations on integral types. These operations are operated on one or more bit patterns according to the level of individual bits. It is very fast as it is directly done by the processor. you can use it for different kind of calculations and manipulation. For low-cost processors bitwise operation is more faster in compare to the arithmetic operation but now a days processors are fast so performance of both operators are same.

& (Binary AND Operator) - If a bit exists in both operands then it copies one bit as a result. That means if both bits are 1 then it returns 1.

| (Binary OR Operator) - If a bit exists in either of operands then it copies one bit as a result. It means if either bit is 1 then it returns 1.

^ (Binary XOR Operator) - It copies the bit if a bit is set in one operand but not both then it copies one bit as a result. It returns 1 if both bits are different.

~ (Binary Ones Complement Operator) - This operator inverts the bits.

<< (Binary Left Shift Operator) - This operator shifts the left operands value by the number of bits specified by the right operand.

>> (Binary Right Shift Operator) - This operator shifts the right operands value by the number of bits specified by the right operand.

>>> (Shift right zero fill operator) - This operator shifts the right operands value by the number of bits specified by the right operand and shifted values are filled up with zeroes.

Example :

package operator;

class BitwiseOperator {
	public static void main(String[] args) {
		int x = 24; 
		int y = 10; 
		int z = 0;

		z = x & y; //Binary AND Operator
		System.out.println("x & y = " + z);

		z = x | y; //Binary OR Operator
		System.out.println("x | y = " + z);

		z = x ^ y; //Binary XOR Operator
		System.out.println("x ^ y = " + z);

		z = ~x; //Binary Ones Complement Operator
		System.out.println("~x = " + z);

		z = x << 2; //Binary Left Shift Operator
		System.out.println("x << 2 = " + z);

		z = x >> 2; //Binary Right Shift Operator
		System.out.println("x >> 2  = " + z);

		z = x >>> 2; //Shift right zero fill operator
		System.out.println("x >>> 2 = " + z);
	}
}

Output :

x & y = 8
x | y = 26
x ^ y = 18
~x = -25
x << 2 = 96
x >> 2  = 6
x >>> 2 = 6
Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 20, 2012

Ask Questions?    Discuss: Bitwise Operators in java 7  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com