In this lab you will use bitwise operators to access and manipulate individual bits in integer values.
Every primitive integer value in Java is represented as a sequence of bits. Each bit is a base-2 digit that can represent either 0 or 1. The bits may be numbered from 0..n-1, where n is the number of bits in the value. For an integer composed of bits b_{0}..b_{n-1}, the numeric value of the integer is computed by the following polynomial:
2^{n-1}⋅b_{n-1} + 2^{n-2}⋅b_{n-2} + ... + 2^{1}⋅b_{1} + 2^{0}⋅b_{0}
In other words, if bit j is set to 1, it contributes 2^{j} to the overall value of the integer.
Java supports several primitive integer data types. Each has a fixed number of bits:
Type Bits Signed? byte 8 yes short 16 yes char 16 no int 32 yes long 64 yes
Sometimes, it is useful to be able to manipulate an individual bit or bits in an integer value. The operators to directly access and manipulate the bits of an integer are referred to as bitwise operators.
The following table summarizes the three most important bitwise operators: bitwise and, bitwise or, and bitwise exclusive or. Each of these operators takes two integer operands, and computes a function (and/or/exclusive or) on each identically-numbered pair of bits in the two operands. The computed function is shown as a truth table for each operator:
Name Operator Truth table Name Operator Truth table Name Operator Truth table a b a & b a b a | b a b a ^ b Bitwise
And& 0 0 0 Bitwise
Or| 0 0 0 Bitwise
Exclusive
Or^ 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 0
Stated another way:
As an example, here is the result of computing bitwise or, and, and exclusive or on two 8-bit integer values. (Bit 7 is shown on the left and bit 0 on the right.)
01001000 01001000 01001000 & 01100101 | 01100101 ^ 01100101 -------- -------- -------- 01000000 01101101 00101101
The shift operators, << and >>, move each bit in a value to the left or right. The following table shows examples of applying a shift to 8-bit integer values:
Expression Meaning Example Result value << m shift value m bits to the left 01001000 << 2 00100000 value >> m shift value m bits to the right 01001000 >> 2 00010010
Shift operators discard some bits from the value. For example, shifting a value 2 bits to the left discards the two left-most bits, and shifting a value 2 bits to the right discards the two right-most bits.
Finally, the bitwise complement operator, ~, "toggles" each bit in a single integer operand: the 1 bits become 0 and the 0 bits become 1. Here is an example of taking the bitwise complement of an 8-bit integer value:
Example Result ~ 01001000 10110111
Your task is to implement three methods of the Bitwise class: setBit, clearBit, and toggleBit. These methods should work as follows:
- public static int setBit(int value, int m)
- Return the value formed by setting bit m in value to 1. (If bit m is already 1, then value is returned unchanged.)
- public static int clearBit(int value, int m)
- Return the value formed by setting bit m in value to 0. (If bit m is already 0, then value is returned unchanged.)
- public static int toggleBit(int value, int m)
- Return the value formed by toggling bit m in value. (If bit m is 1, then it should be set to 0. If bit m is 0, then it should be set to 1. All other bits should remain unchanged.)
Hint: an integer value with bit m set to 1 and all other bits set to 0 may be obtained using the expression
1 << m
You can use this value in conjunction with bitwise operators to implement each of the methods specified above.
To get started, import the project lab11.zip into your Eclipse workspace.
To test your methods, right click on "TestBitwise.java" and choose "Run As->JUnit Test". You are done when all of the JUnit tests pass.
To submit, run the following commands from a terminal window:
cd cd eclipse-workspace submit102 lab11