BIT WISE OPERATORS

CONTENTS

1. Overview
2. Example program


1. OVERVIEW

The Java language includes a number of bit wise opertors. These are listed in Table 1.

OperatorFunction
x&yBit wise AND
x|yBit wise OR
x^yBit wise exclusive or (XOR)
~xComplementD
x<<nLeft shift
x>>nRight shift
x>>>nRight shift (zero infil)

Table 1 Bit wise operators

The complement prefix unary operator (~) toggles each bit in its operand. The left shift infix operator (<<) shifts the bits in the prefix operand n places to the left filling in with zero bits to the right. The right shift infix operator (>>) operates in a similar manner but moving bits to the right and filling in with the most significant bit to the left. Thus given the binary number:

1000

(-8), assuming a 4 bit architecture, 1000 >> 2 would produce 1110. Alternatively the >>> operator when applied to the above number:

1000 >>> 2

will produce 0010, i.e. the operator fills in with zero bits on the left hand side.



2. EXAMPLE PROGRAM

A BinaryOperators class is presented in Table 2 which includes code to demonstrate all of the above bit wise operators.

// BINARY OPERATORS
// Frans Coenen
// 25 January 2000
// Dept Computer Science, University of Liverpool

import java.io.*;

class BinaryOperators {

    /* LOGICAL AND. */
    
    public void logicalAnd(int num1, int num2) {
    	int num3;
	
	outputTwoNumbers("LOGICAL AND",num1,num2);
	num3 = num1 & num2;
	dec2bin(num3);
	System.out.println();
	}

    /* LOGICAL OR. */
    
    public void logicalOr(int num1, int num2) {
    	int num3;
	
	outputTwoNumbers("LOGICAL OR",num1,num2);
	num3 = num1 | num2;
	dec2bin(num3);
	System.out.println();
	}
	
    /* LOGICAL XOR. */
    
    public void logicalXOr(int num1, int num2) {
    	int num3;
	
	outputTwoNumbers("LOGICAL XOR",num1,num2);
	num3 = num1 ^ num2;
	dec2bin(num3);
	System.out.println();
	}
	
    /* COMPLEMENT */
     
    public void complement(int num) {
        
	System.out.println("COMPLEMENT");
	dec2bin(num);
	num = ~num;
	dec2bin(num);
	System.out.println();
        }
	
    /* LEFT SHIFT */
     
    public void leftShift(int num) {
        
	System.out.println("LEFT SHIFT");
	while (num != 0) {
	    dec2bin(num);
	    num = num << 1;
	    }
	
	System.out.println();
        }

    /* RIGHT SHIFT 1 */
     
    public void rightShift1(int num) {
        
	System.out.println("RIGHT SHIFT 1");
	while (num != -1 && num != 0) {
	    dec2bin(num);
	    num = num>>1;
	    }
	
	dec2bin(num);    
	System.out.println();
        }
	
    /* RIGHT SHIFT 2 */
     
    public void rightShift2(int num) {
        
	System.out.println("RIGHT SHIFT 2");
	while (num != 0) {
	    dec2bin(num);
	    num = num>>>1;
	    }
	    
	System.out.println();
        }
		  	
    /* OUTPUT TWO NUMBERS */
    	
    private void outputTwoNumbers(String label, int num1, int num2) {
        System.out.println(label);
	dec2bin(num1);
	dec2bin(num2);
	System.out.println("-------------------------------------------------");
        }
	
    /* DEC TO BIN */
    
    private void dec2bin(int number) {
    	int digit = Integer.MIN_VALUE;
	
	for(int counter=0;counter<32;counter++) {
	    if ((number & digit) == 0) System.out.print(0);
	    else System.out.print(1);
    	    digit = digit>>>1;
	    }
	    
	System.out.println(" (" + number + ")");    
        }
    }

Table 2: Bit wise operators

To use this class we of course need an application class; this is given in Table 3

// BINARY OPERATORS APPLICATION
// Frans Coenen
// 25 January 2000
// Dept Computer Science, University of Liverpool

import java.io.*;

class BinOpsApp{

    // ------------------- FIELDS ------------------------ 
                                           
    // Create BufferedReader class instance

    public  static BufferedReader    keyboardInput = new 
                      BufferedReader(new InputStreamReader(System.in));
		      
    // ------------------ METHODS ------------------------  
    
    /* Main method */
    
    public static void main(String[] args) throws IOException {
        int n1, n2;
                             
        // Input two numbers

	System.out.println("Input two numbers");
	n1 = new Integer(keyboardInput.readLine()).intValue();
	n2 = new Integer(keyboardInput.readLine()).intValue();
        BinaryOperators newBinOps = new BinaryOperators();
	                     
        // Binary logical operators

        newBinOps.logicalAnd(n1,n2);
	newBinOps.logicalOr(n1,n2);
	newBinOps.logicalXOr(n1,n2);

	// Unary logical operators
	
	newBinOps.complement(n1);
	
	// Shifts
	
	newBinOps.leftShift(n1);
	newBinOps.rightShift1(n1);
	newBinOps.rightShift2(n1);
        }
    }

Table 3: Application class for bit wise operators example code

Some sample output produced by the above code is given in Table 4.

$ java BinOpsApp
1234Input two numbers
56789
123456789
LOGICAL AND
00000111010110111100110100010101 (123456789)
00000111010110111100110100010101 (123456789)
-------------------------------------------------
00000111010110111100110100010101 (123456789)

LOGICAL OR
00000111010110111100110100010101 (123456789)
00000111010110111100110100010101 (123456789)
-------------------------------------------------
00000111010110111100110100010101 (123456789)

LOGICAL XOR
00000111010110111100110100010101 (123456789)
00000111010110111100110100010101 (123456789)
-------------------------------------------------
00000000000000000000000000000000 (0)

COMPLEMENT
00000111010110111100110100010101 (123456789)
11111000101001000011001011101010 (-123456790)      

LEFT SHIFT
00000111010110111100110100010101 (123456789)
00001110101101111001101000101010 (246913578)
00011101011011110011010001010100 (493827156)
00111010110111100110100010101000 (987654312)
01110101101111001101000101010000 (1975308624)
11101011011110011010001010100000 (-344350048)
11010110111100110100010101000000 (-688700096)
10101101111001101000101010000000 (-1377400192)
01011011110011010001010100000000 (1540166912)
10110111100110100010101000000000 (-1214633472)
01101111001101000101010000000000 (1865700352)
11011110011010001010100000000000 (-563566592)
10111100110100010101000000000000 (-1127133184)
01111001101000101010000000000000 (2040700928)
11110011010001010100000000000000 (-213565440)
11100110100010101000000000000000 (-427130880)
11001101000101010000000000000000 (-854261760)
10011010001010100000000000000000 (-1708523520)
00110100010101000000000000000000 (877920256)
01101000101010000000000000000000 (1755840512)
11010001010100000000000000000000 (-783286272)
10100010101000000000000000000000 (-1566572544)
01000101010000000000000000000000 (1161822208)
10001010100000000000000000000000 (-1971322880)
00010101000000000000000000000000 (352321536)
00101010000000000000000000000000 (704643072)
01010100000000000000000000000000 (1409286144)
10101000000000000000000000000000 (-1476395008)
01010000000000000000000000000000 (1342177280)
10100000000000000000000000000000 (-1610612736)
01000000000000000000000000000000 (1073741824)
10000000000000000000000000000000 (-2147483648)

RIGHT SHIFT 1           
00000111010110111100110100010101 (123456789)
00000011101011011110011010001010 (61728394)
00000001110101101111001101000101 (30864197)
00000000111010110111100110100010 (15432098)
00000000011101011011110011010001 (7716049)
00000000001110101101111001101000 (3858024)
00000000000111010110111100110100 (1929012)
00000000000011101011011110011010 (964506)
00000000000001110101101111001101 (482253)
00000000000000111010110111100110 (241126)
00000000000000011101011011110011 (120563)
00000000000000001110101101111001 (60281)
00000000000000000111010110111100 (30140)
00000000000000000011101011011110 (15070)
00000000000000000001110101101111 (7535)
00000000000000000000111010110111 (3767)
00000000000000000000011101011011 (1883)
00000000000000000000001110101101 (941)
00000000000000000000000111010110 (470)
00000000000000000000000011101011 (235)
00000000000000000000000001110101 (117)
00000000000000000000000000111010 (58)
00000000000000000000000000011101 (29)
00000000000000000000000000001110 (14)
00000000000000000000000000000111 (7)
00000000000000000000000000000011 (3)
00000000000000000000000000000001 (1)
00000000000000000000000000000000 (0)

RIGHT SHIFT 2      
00000111010110111100110100010101 (123456789)
00000011101011011110011010001010 (61728394)
00000001110101101111001101000101 (30864197)
00000000111010110111100110100010 (15432098)
00000000011101011011110011010001 (7716049)
00000000001110101101111001101000 (3858024)
00000000000111010110111100110100 (1929012)
00000000000011101011011110011010 (964506)
00000000000001110101101111001101 (482253)
00000000000000111010110111100110 (241126)
00000000000000011101011011110011 (120563)
00000000000000001110101101111001 (60281)
00000000000000000111010110111100 (30140)
00000000000000000011101011011110 (15070)
00000000000000000001110101101111 (7535)
00000000000000000000111010110111 (3767)
00000000000000000000011101011011 (1883)
00000000000000000000001110101101 (941)
00000000000000000000000111010110 (470)
00000000000000000000000011101011 (235)
00000000000000000000000001110101 (117)
00000000000000000000000000111010 (58)
00000000000000000000000000011101 (29)
00000000000000000000000000001110 (14)
00000000000000000000000000000111 (7)
00000000000000000000000000000011 (3)
00000000000000000000000000000001 (1)   

Table 4: Sample output generate from bit wise operators example code




Created and maintained by Frans Coenen. Last updated 29 March 2000