Bitwise logical operators

advertisement
CHAPTER 5-5
JAVA BITWISE OPERATORS
Bitwise logical operators



Java provides 4 bitwise logical operators: & (and), | (or), ~ (not), ^ (exclusive or)
The bitwise operators manipulate individual bits. Their operands must be integral.
They behave according to the following table. These operators produce no side effects.
x y
x&y
x^y
x|y
~x
0 0
0
0
0
1
1 0
0
1
1
0
0 1
0
1
1
1
1 1
1
0
1
0
//Example 5-5-1 – assume 8-bit word
Suppose byte x is 01110011 (115), byte y is 00110111 (55).
x&y:
01110011 (x=115)
00110111 (y=55)
------------
x^y:
01110011 (x=115)
00110111 (y=55)
------------
x|y:
01110011 (x=115)
00110111 (y=55)
------------
~x:
01110011 (x=115)
------------
//Example 5-5-2
//Demo.java
class Demo
{
public static void main(String[] args)
{
int i=115, j=55;
System.out.println(i&j);
System.out.println(i^j);
System.out.println(i|j);
System.out.println(~i);
}
}
Bitwise shift operators




Java provides 3 bitwise shift operators: << (left shift), >> (signed right shift), >>> (unsigned right
shift)
The expression x<<y shifts x left by y bits, with 0's shift in. It has the effect of multiplying integer x
by ___.
The expression x>>y shifts x right by y bits, with the sign bit shifts in. It has the effect of a signed
integer division of dividing integer x by ____.
The expression x>>>y shifts x right by y bits, with 0's shift in. It has the effect of an unsigned division
of dividing x by ____.
//Example 5-5-3 – assume 8-bit word
a. Suppose byte x1 is 00000011
x1<<2 is
b. Suppose byte x2 is 11111111
x2<<2 is
c. Suppose byte x3 is 00001100
x3>>2 is
d. Suppose byte x4 is 10001100
x4>>2 is
e. Suppose byte x5 is 10001111
x5>>2 is
f. Suppose byte x6 is 10001100
x6>>>2 is
//Example 5-5-4
//Demo.java
class Demo
{
public static void main(String[] args)
{
int x1=3, x2=-1, x3=12, x4=-116, x5=-113, x6=-1;
System.out.println(x1<<2);
System.out.println(x2<<2);
System.out.println(x3>>2);
System.out.println(x4>>2);
System.out.println(x5>>2);
System.out.println(x6>>>2);
}
}
Mixed type bitwise expressions

If the bitwise expression is mixed type, Java will use the following type conversion rule:
1. If 1 of the operands is of type long and the other is not, the other operand is converted to long.
The type of the expression is long.
2. Otherwise, both operands are converted to int. The type of the expression is int. This means that
byte or short or char values are always promoted to at least an int in a bitwise expression,
regardless of whether the expression is mixed-typed or not.
In the case of the unary operator ~, java converts a byte, short, or char to an int before applying the
operator, and the resulting value is an int.
Printing internal bit representations
//Example 5-5-5
//BitWise.java
public class BitWise
{
public static void printbit(int n)
{
int i, mask=0x80000000;
for (i=1; i<=32; ++i)
{
System.out.print(((n&mask)==0)?'0':'1');
n<<=1;
}
System.out.println();
}
}
//Example 5-5-6
//Demo.java
class Demo
{
public static void main(String[] args)
{
int i=115, j=55;
System.out.print("i:\t");
BitWise.printbit(i);
System.out.print("j:\t");
BitWise.printbit(j);
System.out.print("i&j:\t");
BitWise.printbit(i&j);
System.out.print("i^j:\t");
BitWise.printbit(i^j);
System.out.print("i|j:\t");
BitWise.printbit(i|j);
System.out.print("~i:\t");
BitWise.printbit(~i);
}
}
//115
//55
//51
//68
//119
//-116
//Example 5-5-7
//Demo.java
class Demo
{
public static void main(String[] args)
{
int x1=3, x2=-1, x3=12, x4=-116, x5=-113, x6=-1;
System.out.print("x1:\t");
BitWise.printbit(x1);
//3
System.out.print("x1<<2:\t");
BitWise.printbit(x1<<2);
//12
System.out.print("x2:\t");
BitWise.printbit(x2);
System.out.print("x2<<2:\t");
BitWise.printbit(x2<<2);
System.out.print("x3:\t");
BitWise.printbit(x3);
System.out.print("x3>>2:\t");
BitWise.printbit(x3>>2);
System.out.print("x4:\t");
BitWise.printbit(x4);
System.out.print("x4>>2:\t");
BitWise.printbit(x4>>2);
System.out.print("x5:\t");
BitWise.printbit(x5);
System.out.print("x5>>2:\t");
BitWise.printbit(x5>>2);
System.out.print("x6:\t");
BitWise.printbit(x6);
System.out.print("x6>>>2:\t");
BitWise.printbit(x6>>>2);
//-1
//-4
//12
//3
//-116
//-29
//-113
//-29
//-1
//1073741823
}
}
Extracting a bit
//Example 5-5-8
//Demo.java
import java.util.Scanner;
class Demo
{
public static void main(String[] args)
{
Scanner Keyboard=new Scanner(System.in);
int mask=1;
System.out.print("Enter an integer:");
int num=Keyboard.nextInt();
BitWise.printbit(num);
while (true)
{
System.out.print("Enter the bit position (0-31) from the right (-1 to quit):");
int i=Keyboard.nextInt();
if (i==-1) break;
mask=mask<<i;
if ((num&mask)==0) System.out.println("0");
else System.out.println("1");
}
}
}
Setting a bit
//Example 5-5-9
//Demo.java
import java.util.Scanner;
class Demo
{
public static void main(String[] args)
{
Scanner Keyboard=new Scanner(System.in);
int mask=1;
System.out.print("Enter an integer:");
int num=Keyboard.nextInt();
BitWise.printbit(num);
System.out.print("Enter the bit position (0-31) from the right:");
int i=Keyboard.nextInt();
mask=mask<<i;
num=num|mask;
BitWise.printbit(num);
}
}
Programming Exercises
1.
Write the following program.
Email/Hand in:
 Demo.java
Download