Bitwise Operations in Android

Frequently used code samples and tutorials
vivekkalady
Site Admin
Posts: 35
Joined: Tue Nov 18, 2014 2:39 pm

Bitwise Operations in Android

Postby vivekkalady » Sun Nov 23, 2014 2:14 pm

Bitwise operators are operators (just like +, *, &&, etc.) that operate on ints and uints at the binary level. This means they look directly at the binary digits or bits of an integer.

  • & (bitwise AND)
  • | (bitwise OR)
  • ~ (bitwise NOT)
  • ^ (bitwise XOR)
  • << (bitwise left shift)
  • >> (bitwise right shift)
  • >>> (bitwise unsigned right shift)
  • &= (bitwise AND assignment)
  • |= (bitwise OR assignment)
  • ^= (bitwise XOR assignment)
  • <<= (bitwise left shift and assignment)
  • >>= (bitwise right shift and assignment)
  • >>>= (bitwise unsigned right shift and assignment)

Bitwise Operations are faster when compared with normal operations

eg :-

Code: Select all

var randInt:int = int(Math.random()*1000);
if(randInt & 1)
{
    trace("Odd number.");
}
else
{
    trace("Even number.");
}


the above code is to find if the number is odd or even.it is about 66% faster than the normal operation number % 2 ==0




Example Code


Code: Select all

/**
     * AND is represented by & such as num1 & num2
     */
    public static int doBitAND(int a, int b) {
        System.out.println("{BITWISE AND OPERATION}\nFirst Number: " + a + "("
                + Integer.toBinaryString(a) + ") Second Number: " + b + "("
                + Integer.toBinaryString(b) + ")");
 
        int result = a & b;
 
        System.out.println("Result: " + result + "("
                + Integer.toBinaryString(result) + ")\n");
 
        return result;
 
    }
 
    /**
     * OR is represented by | such as num1 | num2
     */
    public static int doBitOR(int a, int b) {
        System.out.println("{BITWISE OR OPERATION}\nFirst Number: " + a + "("
                + Integer.toBinaryString(a) + ") Second Number: " + b + "("
                + Integer.toBinaryString(b) + ")");
 
        int result = a | b;
 
        System.out.println("Result: " + result + "("
                + Integer.toBinaryString(result) + ")\n");
 
        return result;
 
    }
 
    /**
     * OR is represented by | such as num1 ^ num2
     */
    public static int doBitXOR(int a, int b) {
 
        System.out.println("{BITWISE XOR OPERATION}\nFirst Number: " + a + "("
                + Integer.toBinaryString(a) + ") Second Number: " + b + "("
                + Integer.toBinaryString(b) + ")");
 
        int result = a ^ b;
 
        System.out.println("Result: " + result + "("
                + Integer.toBinaryString(result) + ")\n");
 
        return result;
 
    }
 
    /**
     * COMPLEMENT is represented by ~ such as ~num
     */
    public static int doBitComplement(int a) {
 
        System.out.println("{BITWISE COMPLEMENT OPERATION}\nInput Number: " + a
                + "(" + Integer.toBinaryString(a) + ")");
 
        int result = ~a;
 
        System.out.println("Result: " + result + "("
                + Integer.toBinaryString(result) + ")\n");
 
        return result;
    }
 
    /**
     * Left shift operation is represented with <<
     * This operation preserves sign.
     * Note: one bit left shift is equivalent to number multiplied by two.
     */
    public static int doBitLeftShift(int a, int numberOfShifts) {
 
        System.out.println("{BITWISE LEFT SHIFT OPERATION}\nInput Number: " + a
                + "(" + Integer.toBinaryString(a) + ") Number of bit shifts: "
                + numberOfShifts);
 
        int result = a << numberOfShifts;
 
        System.out.println("Result: " + result + "("
                + Integer.toBinaryString(result) + ")\n");
 
        return result;
 
    }
 
    /**
     * Right shift operation is represented with >>
     * This operation preserves sign.
     */
    public static int doBitRightShift(int a, int numberOfShifts) {
 
        System.out.println("{BITWISE RIGHT SHIFT OPERATION}\nInput Number: "
                + a + "(" + Integer.toBinaryString(a)
                + ") Number of bit shifts: " + numberOfShifts);
 
        int result = a >> numberOfShifts;
 
        System.out.println("Result: " + result + "("
                + Integer.toBinaryString(result) + ")\n");
 
        return result;
 
    }
 
    /**
     * Unsigned Right shift operation is represented with >>>
     * This operation does not preserve sign.
     */
    public static int doBitUnsignedRightShift(int a, int numberOfShifts) {
        System.out
                .println("{BITWISE UNSIGNED RIGHT SHIFT OPERATION}\nInput Number: "
                        + a
                        + "("
                        + Integer.toBinaryString(a)
                        + ") Number of bit shifts: " + numberOfShifts);
 
        int result = a >>> numberOfShifts;
 
        System.out.println("Result: " + result + "("
                + Integer.toBinaryString(result) + ")\n");
 
        return result;
    }
 
    /**
     * To set nth bit to zero, we follow the following: a = a | (1 << n);
     * Note: bit count starts with index zero from right.
     */
    public static int setNthBitToOne(int a, int whichBit) {
        System.out
                .println("{BITWISE SET NTH BIT TO ONE OPERATION}\nInput Number: "
                        + a
                        + "("
                        + Integer.toBinaryString(a)
                        + ") Bit to set one: " + whichBit);
 
        int result = a = a | (1 << whichBit);
 
        System.out.println("Result: " + result + "("
                + Integer.toBinaryString(result) + ")\n");
 
        return result;
    }
 
    /**
     * To set nth bit to one, we follow the following: a & ~(1 << n);
     * Note: bit count starts with index zero from right.
     */
    public static int setNthBitToZero(int a, int whichBit) {
        System.out
                .println("{BITWISE SET NTH BIT TO ZERO OPERATION}\nInput Number: "
                        + a
                        + "("
                        + Integer.toBinaryString(a)
                        + ") Bit to set zero: " + whichBit);
 
        int result = a & ~(1 << whichBit);
 
        System.out.println("Result: " + result + "("
                + Integer.toBinaryString(result) + ")\n");
 
        return result;
    }


Result

Code: Select all

{BITWISE AND OPERATION}
First Number: 5(101) Second Number: 6(110)
Result: 4(100)
 
{BITWISE AND OPERATION}
First Number: -5(11111111111111111111111111111011) Second Number: 6(110)
Result: 2(10)
 
{BITWISE OR OPERATION}
First Number: 5(101) Second Number: 6(110)
Result: 7(111)
 
{BITWISE OR OPERATION}
First Number: -5(11111111111111111111111111111011) Second Number: 6(110)
Result: -1(11111111111111111111111111111111)
 
{BITWISE XOR OPERATION}
First Number: 5(101) Second Number: 6(110)
Result: 3(11)
 
{BITWISE XOR OPERATION}
First Number: -5(11111111111111111111111111111011) Second Number: 6(110)
Result: -3(11111111111111111111111111111101)
 
{BITWISE COMPLEMENT OPERATION}
Input Number: 5(101)
Result: -6(11111111111111111111111111111010)
 
{BITWISE COMPLEMENT OPERATION}
Input Number: -5(11111111111111111111111111111011)
Result: 4(100)
 
{BITWISE LEFT SHIFT OPERATION}
Input Number: 5(101) Number of bit shifts: 1
Result: 10(1010)
 
{BITWISE LEFT SHIFT OPERATION}
Input Number: -5(11111111111111111111111111111011) Number of bit shifts: 2
Result: -20(11111111111111111111111111101100)
 
{BITWISE RIGHT SHIFT OPERATION}
Input Number: 5(101) Number of bit shifts: 1
Result: 2(10)
 
{BITWISE RIGHT SHIFT OPERATION}
Input Number: -5(11111111111111111111111111111011) Number of bit shifts: 2
Result: -2(11111111111111111111111111111110)
 
{BITWISE UNSIGNED RIGHT SHIFT OPERATION}
Input Number: 5(101) Number of bit shifts: 1
Result: 2(10)
 
{BITWISE UNSIGNED RIGHT SHIFT OPERATION}
Input Number: -5(11111111111111111111111111111011) Number of bit shifts: 2
Result: 1073741822(111111111111111111111111111110)
 
{BITWISE SET NTH BIT TO ZERO OPERATION}
Input Number: 5(101) Bit to set zero: 2
Result: 1(1)
 
{BITWISE SET NTH BIT TO ZERO OPERATION}
Input Number: -5(11111111111111111111111111111011) Bit to set zero: 1
Result: -7(11111111111111111111111111111001)
 
{BITWISE SET NTH BIT TO ONE OPERATION}
Input Number: 5(101) Bit to set one: 1
Result: 7(111)
 
{BITWISE SET NTH BIT TO ONE OPERATION}
Input Number: -5(11111111111111111111111111111011) Bit to set one: 2
Result: -1(11111111111111111111111111111111)

Return to “Tutorials & Examples”

Who is online

Users browsing this forum: No registered users and 1 guest

cron