Java method to perform various bitwise operations on two integers and display the results : Bitwise Operators « Operators « SCJP

Home
SCJP
1.Java Source And Data Type
2.Operators
3.Modifiers
4.Type Casting
5.Statements
6.Object Oriented
7.Thread
8.Utility Classes
9.File
SCJP » Operators » Bitwise Operators 
2.3.9.Java method to perform various bitwise operations on two integers and display the results
public class MainClass {
  public static void main(String[] argv) {
    test(12);
  }

  static void test(int a, int b) {
    int c = a & b;
    System.out.println("Result AND & " + Integer.toBinaryString(c" = " + c);
    c = a | b;
    System.out.println("Result OR  | " + Integer.toBinaryString(c" = " + c);
    c = a ^ b;
    System.out.println("Result XOR ^ " + Integer.toBinaryString(c" = " + c);
    System.out.println("Complement of " + a + " = " + Integer.toBinaryString(~a));
    System.out.println("Complement of " + b + " = " + Integer.toBinaryString(~b));
  }
}
Result AND & 0 = 0
Result OR  | 11 = 3
Result XOR ^ 11 = 3
Complement of 1 = 11111111111111111111111111111110
Complement of 2 = 11111111111111111111111111111101
2.3.Bitwise Operators
2.3.1.Bitwise Operator Summary
2.3.2.Shift Operator Summary
2.3.3.The Bitwise Inversion Operator ~ performs bitwise inversion on integral types.
2.3.4.The Boolean Complement Operator ! inverts the value of a boolean expression.
2.3.5.The bitwise operator & provides bitwise AND
2.3.6.The bitwise operator ^ provides eXclusive-OR (XOR)
2.3.7.The bitwise operator | provides OR operations
2.3.8.Here is an example of the use of the & operator:
2.3.9.Java method to perform various bitwise operations on two integers and display the results
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.