Pacific-Design.com

    
Home Index

1. Algorithms

2. Bitwise

Algorithms / Bitwise /

Math Operators

public class Main {

    public static void main(String args[]) {
        System.out.println("\nBitwise Operations");
        Bitwise();
        
        System.out.println("\nMath Operators");
        Operators();
    }

    static void Bitwise() {
        int a = 2;
        int b = 5;
        int c = 0;

        c = a & b;
        System.out.println("a & b   = " + c);

        c = a | b;
        System.out.println("a | b   = " + c);

        c = a ^ b;
        System.out.println("a ^ b   = " + c);

        c = ~a;
        System.out.println("~a      = " + c);

        c = a << 8;
        System.out.println("a << 2  = " + c);

        c = a >> 1;
        System.out.println("a >> 2  = " + c);

        c = a >>> 2;
        System.out.println("a >>> 2 = " + c);

    }

    static void Operators() {
        int a = 10;
        int b = 20;
        int c = 0;
        
        c = a + b;        
        System.out.println("c = a + b = " + c); 
        
        c += a;        
        System.out.println("c += a = " + c);
        
        c -= a;        
        System.out.println("c -= a = " + c);
        
        c *= a;
        System.out.println("c *= a = " + c);
        
        a = 10;
        c = 15;
        c /= a;
        System.out.println("c /= a = " + c);

        a = 10;
        c = 15;
        c %= a;
        System.out.println("c %= a = " + c);
        
        c <<= 2;
        System.out.println("c <<= 2 = " + c);
        
        c >>= 2;
        System.out.println("c >>= 2 = " + c);
        
        c >>= 2;
        System.out.println("c >>= a = " + c);
        
        c &= a;
        System.out.println("c &= 2 = " + c);
        
        c ^= a;
        System.out.println("c ^= a = " + c);
        
        c |= a;
        System.out.println("c |= a = " + c);
    }
}

/*
Output

Bitwise Operations
a & b   = 0
a | b   = 7
a ^ b   = 7
~a      = -3
a << 2  = 512
a >> 2  = 1
a >>> 2 = 0

Math Operators
c = a + b = 30
c += a = 40
c -= a = 30
c *= a = 300
c /= a = 1
c %= a = 5
c <<= 2 = 20
c >>= 2 = 5
c >>= a = 1
c &= 2 = 0
c ^= a = 10
c |= a = 10
*/