Operators in Java(part4)
Hi readers,This is my seventh article and it is the fourth part of operators in Java.On my previous articles I have discussed types of operators with in examples.This is the last part of Operators,and it is Bit Wise operators.
Bit Wise Operators
Bit wise operators in java are shown in below diagram.
Expressions of AND(&),OR(|) Operators are shown in below diagram.
We have to convert decimal numbers in to binary numbers when we use AND,OR and XOR operators.Result is coming as decimal number,but the process is there with binary numbers.With in go through following examples you may able to study about BitWise operators.
AND operator
int a=25; // 1 1 0 0 1 (binary form of 25)
int b=15; // 0 1 1 1 1 (binary form of 15) int c=a&b;// 0 1 0 0 1
System.out.println(c); // 9
OR operator
int a=25; // 1 1 0 0 1
int b=15; // 0 1 1 1 1int c=a|b; // 1 1 1 1 1
System.out.println(c); // 31
XOR operator
int a=25; // 1 1 0 0 1
int b=15; // 0 1 1 1 1int c=a^b; // 1 0 1 1 0System.out.println(c); // 22
Bit Wise Compliment
int number=35,result;
result=~number;
System.out.println(result);// -36
Why -36 has come as result?35 -> 0 0 1 0 0 0 1 1
220 -> 1 1 0 1 1 1 0 0 // The compliment of 35
-36 -> - 0 0 1 0 0 1 0 0 // 2’s compliment of 35
Signed left shift
int num=212; // 1 1 0 1 0 1 0 0
System.out.println(212<<1); // 1 1 0 1 0 1 0 0 0 // 424
System.out.println(212<<0); // 1 1 0 1 0 1 0 0 // 212
System.out.println(212<<4); // 1 1 0 1 0 1 0 0 0 0 0 0 // 3392
Signed Right shift
int num=212;
System.out.println(212>>1); // 1 1 0 1 0 1 0 //106 System.out.println(212>>0);// 1 1 0 1 0 1 0 0 //212
System.out.println(212>>4); // 1 1 0 1 // 13
In my this blog I have discussed mainly about BitWise Operators in Java.I have completed about operators in Java with in four articles.In my next article I hope to discuss about loops in java,step by step.Thank you for reading.Keep in touch with hoping the next.