bitwise operators in java with example programs

The Program is look like this:



class BitLogic{
 public static void main(String[] args) {
            String binary[] = {
      "00000", "00001", "00010", "00011", "00100", "00101", "00110", "00111", "01000", "10010", "10100", "10110", "11000", "11010", "11100", "11110" };
    int a = 3; // 0 + 2 + 1 or 0011 in binary
    int b = 6; // 4 + 2 + 0 or 0110 in binary
    int c = a | b;
    int d = a & b;
    int e = a ^ b;
int f = (~a & b) | (a & ~b);
int g = ~a & 0x0f;
System.out.println("        a = " + binary[a]);
    System.out.println("        b = " + binary[b]);
    System.out.println("      a|b = " + binary[c]);
    System.out.println("      a&b = " + binary[d]);
    System.out.println("      a^b = " + binary[e]);
    System.out.println("~a&b|a&~b = " + binary[f]);
    System.out.println("       ~a = " + binary[g]);

    }
   
}

Comments

Popular posts from this blog

Inheritance in Java

Operators in Java Language?

Type Casting or Conversion in Java