Skip to main content

// Birwise Operator

 // Birwise Operator


  1. class bit //class Declaration; class name same as file name.
  2. {
  3. public static void main(String args[])
  4. {
  5. int a = 50;   //110010 in binary.
  6. int b = 30;  //11110 in binary.
  7. int c = 0;  // initializing variables
  8. int d = 0;
  9. int e = 0;
  10. int f = 0;
  11. int g = 0;
  12. int h = 0;
  13. int i = 0;

  14. //Bitwise AND 
  15. c = a & b; // 110010 & 11110 = 10010 i.e means is '18'.
  16. System.out.println("the value of (a & b):"+c);

  17. //Bitwise OR
  18. d = a | b; // 110010 | 11110 = 111110 i.e means is '62'.
  19. System.out.println("the value of (a | b):"+d);

  20. //Bitwise Exclusive XOR
  21. e = a ^ b; // 110010 ^ 11110 = 101100 i.e means is '44'.
  22. System.out.println("the value of (a ^ b):"+e);

  23. //Bitwise Complement of a
  24. f = ~a; // -(50+1)= i.e means is '-51'.
  25. System.out.println("the value of (~a):"+f);

  26. //Bitwise Complement of b
  27. i = ~b; //-(30+1)= i.e means is '-31'.
  28. System.out.println("the value of (~b):"+i);

  29. //Bitwise Shift Left
  30. g = a << 2; //50=110010 << 2 :- 11001000 i.e means is '200'. //Shift with 2 zero to left side.
  31. System.out.println("the value of (a << b ):"+g);

  32. //Bitwise Shift Right
  33. h = a >>> 2; // 50=110010 >>> 2 :-  001100 i.e means os '12'. //Shift with 2 zero to right side.
  34. System.out.println("the value of (a >>> b):"+h);

  35. }
  36. }


//Execute


//Output

/*

the value of (a & b):18

the value of (a | b):62

the value of (a ^ b):44

the value of (~a):-51

the value of (~b):-31

the value of (a << b ):200

the value of (a >> b):12

*/


                                                 //ThE ProFessoR