0

Can any one tell me how to work bitwise operators with suitable example.?

7th Jan 2017, 6:28 PM
ajay tiwari
ajay tiwari - avatar
4 Answers
+ 1
The Bitwise Operators Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte. Bitwise operator works on bits and performs bit-by-bit operation. Assume if a = 60 and b = 13; now in binary format they will be as follows − a = 0011 1100 b = 0000 1101 ----------------- a&b = 0000 1100 a|b = 0011 1101 a^b = 0011 0001 ~a = 1100 0011 The following table lists the bitwise operators − Assume integer variable A holds 60 and variable B holds 13 then − The following program is a simple example that demonstrates the bitwise operators. Copy and paste the following Java program in Test.java file and compile and run this program − Example public class Test { public static void main(String args[]) { int a = 60; /* 60 = 0011 1100 */ int b = 13; /* 13 = 0000 1101 */ int c = 0; c = a & b; /* 12 = 0000 1100 */ System.out.println("a & b = " + c ); c = a | b; /* 61 = 0011 1101 */ System.out.println("a | b = " + c ); c = a ^ b; /* 49 = 0011 0001 */ System.out.println("a ^ b = " + c ); c = ~a; /*-61 = 1100 0011 */ System.out.println("~a = " + c ); c = a << 2; /* 240 = 1111 0000 */ System.out.println("a << 2 = " + c ); c = a >> 2; /* 15 = 1111 */ System.out.println("a >> 2 = " + c ); c = a >>> 2; /* 15 = 0000 1111 */ System.out.println("a >>> 2 = " + c ); } } This will produce the following result − Output a & b = 12 a | b = 61 a ^ b = 49 ~a = -61 a << 2 = 240 a >> 15 a >>> 15 Hope this helps :) Happy Coding!
7th Jan 2017, 7:05 PM
TheJaguar
0
Hey ajay tiwari! I'm sure a lot of people can be right as rain to tell you the answer to that but before we can we have to know in what system you want to use them. is it C++ C# or java? they operate a little differently in each and if you specify we can immediately give you an answer. or simply in your tags write which system the question is for in the future. Happy Coding!
7th Jan 2017, 6:53 PM
TheJaguar
0
Java
7th Jan 2017, 6:55 PM
ajay tiwari
ajay tiwari - avatar
0
thanks
7th Jan 2017, 7:08 PM
ajay tiwari
ajay tiwari - avatar