In this section you will learn how to use "&" operator in java. Java provide six types of operators: among them one is Bitwise operator which can be applied to integer, long, int char short type. "&" operator comes under type of Bitwise operator in java. Bitwise operator work on bit and perform bit by bit operation.
Java "&" operator.
In this section you will learn how to use "&" operator in java. Java provide six types of operators: among them one is Bitwise operator which can be applied to integer, long, int char short type. "&" operator comes under type of Bitwise operator in java. Bitwise operator work on bit and perform bit by bit operation.
Example: let us suppose a=10 and b=20, Now in binary format they will be as follows:
a = 0000 1010
b = 0001 0100
a & b = 0000 0000
"&" operator work on bit , so it will produces 1 if both operand will be 1(1&1). But if both bit are 0 or different (0&1) it will produces 0. In more simple word , if both bit are 1 then it will produce 1 but if one of the bit is 0 then it will produce 0. In the above example we can see that the most last bit in 'a' is 0 and the most last bit in 'b' is 0, so it produces 0 only in result that is in c.
Code to display use of "&" operator in Java: A simple example using Bitwise (AND) "&" operator.
public class operators{ public static void main(String args[]) { int a=10,b=20,c=0; //int a=5,b=6; c=a&b; System.out.println("Result of A & b = "+c); } }In the above example, a is 10 that is 0000 1010 and b is 0001 0100 all bit are different so it produces 0 as output. Not a single bit is same that is 1 in both operand either a or b.
Output:
When you will take the value of a=10 and b=20 then the output will be as follows :
When you will take the value of a=5 and b=6 then the output will be as follows :