| 1 | package Question5_7; |
| 2 | |
| 3 | public class BitInteger { |
| 4 | public static int INTEGER_SIZE; |
| 5 | private boolean[] bits; |
| 6 | public BitInteger() { |
| 7 | bits = new boolean[INTEGER_SIZE]; |
| 8 | } |
| 9 | /* Creates a number equal to given value. Takes time proportional |
| 10 | * to INTEGER_SIZE. */ |
| 11 | public BitInteger(int value){ |
| 12 | bits = new boolean[INTEGER_SIZE]; |
| 13 | for (int j = 0; j < INTEGER_SIZE; j++){ |
| 14 | if (((value >> j) & 1) == 1) bits[INTEGER_SIZE - 1 - j] = true; |
| 15 | else bits[INTEGER_SIZE - 1 - j] = false; |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | /** Returns k-th most-significant bit. */ |
| 20 | public int fetch(int k){ |
| 21 | if (bits[k]) return 1; |
| 22 | else return 0; |
| 23 | } |
| 24 | |
| 25 | /** Sets k-th most-significant bit. */ |
| 26 | public void set(int k, int bitValue){ |
| 27 | if (bitValue == 0 ) bits[k] = false; |
| 28 | else bits[k] = true; |
| 29 | } |
| 30 | |
| 31 | /** Sets k-th most-significant bit. */ |
| 32 | public void set(int k, char bitValue){ |
| 33 | if (bitValue == '0' ) bits[k] = false; |
| 34 | else bits[k] = true; |
| 35 | } |
| 36 | |
| 37 | /** Sets k-th most-significant bit. */ |
| 38 | public void set(int k, boolean bitValue){ |
| 39 | bits[k] = bitValue; |
| 40 | } |
| 41 | |
| 42 | public void swapValues(BitInteger number) { |
| 43 | for (int i = 0; i < INTEGER_SIZE; i++) { |
| 44 | int temp = number.fetch(i); |
| 45 | number.set(i, this.fetch(i)); |
| 46 | this.set(i, temp); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | public int toInt() { |
| 51 | int number = 0; |
| 52 | for (int j = INTEGER_SIZE - 1; j >= 0; j--){ |
| 53 | number = number | fetch(j); |
| 54 | if (j > 0) { |
| 55 | number = number << 1; |
| 56 | } |
| 57 | } |
| 58 | return number; |
| 59 | } |
| 60 | } |
nothing calls this directly
no outgoing calls
no test coverage detected