Various type of operator in java
There are four types of a bitwise operator in java
No | Operator | USD | Description |
---|---|---|---|
1 | & | A & C | Bitwise AND |
2 | | | A|B | Bitwise or |
3 | ^ | A^B | Bitwise XOR |
4 | ~ | C~A | Bitwise complement |
1) Bitwise AND
- Bitwise And operator is a binary operator, The symbol used in the '&'
- This operator internal process is first of normal value is converted in the binary and then a summation of both value and answer should generate binary value then convert the normal value and show the final answer
- Binary both value in 1-bit then give 1-bit otherwise it gives 0-bit value
Example
output
public class BitwiseAnd {
public static void main(String[] args) {
int a =4; //binary value 100
int b= 7; //binary value 111
//sum of two binary value
int c = a & b;
// 1 0 0 (4)
// & 1 1 1 (7)
// -----------
// 1 0 0 (4)
//answer of the binary value
System.out.println("Bitwise And operator value :- "+c);
}
}
Bitwise And operator value :- 4
2) Bitwise OR
- Bitwise OR operator is also binary operator, the symbol is ' | '
- Bitwise OR operator Internal process is the same as the Bitwise AND operator
- Binary both value is 0-bit then 0-bit otherwise the value is 1-bit
Example
public class BitwiseOR {
public static void main(String[] args) {
int a =7; //binary value 111
int b= 3; //binary value 011
int c= a | b ;
// 1 1 1 (7)
// | 0 1 1 (3)
//--------------
// 1 1 1 (7)
//final ans of bitwiseor operation
System.out.println("Bitwise OR operator value :- "+c);
}
}
Output
Bitwise OR operator value :- 7
3)Bitwise XOR
- Bitwise XOR also binary operator , The symbol is ' ^ '
- Bitwise XOR operator internal process is the same as the Bitwise AND operator
- Binary both value(1-bit and 0-bit) is opposite that give 1-bit otherwise give the 0-bit value
Example
public class BitwiseXOR {
public static void main(String[] args) {
int a = 2; //binary value 010
int b = 7; // binary value 111
int c= a ^ b ;
// o 1 0 (2)
// ^ 1 1 1 (7)
//---------------
// 1 0 1 (5)
System.out.println("Bitwise XOR operator value :- "+c);
}
}
Output
Bitwise XOR operator value :- 5
4) Bitwise Complement
- Bitwise Complement operator is also binary operator, the symbol is ' ~ '
- Bitwise complement operator is internal process is same as Bitwise AND operator
- Binary value is performed in the opposite way if the value is 1-bit then convert in 0-bit and 0-bit value is converted in the 1-bit
Example
public class BitwiseComplement {
public static void main(String[] args) {
int a = 2; //binary value 010
// ~ 0 1 0
//-----------------
// 1 0 1
// second compliment of 1 0 1
// final answer print
System.out.println("Bitwise complement operator value :- "+ ~a);
}
}
Output
Bitwise complement operator value :- -3
So it's all about in Bitwise Operators In Java with Example Program
Tags:
operator