I'm having some problems with my bit class and register class for a homework assignment. Here's the first part of the question and my code for it. The code compiles but it doesn't work with the register class. Here's the question for the bit class.

You will write a class named Bit which will have one data ﬁeld for storing the value of

the bit. Since bits have only two possible values, you will use boolean for the data type.

You will decide whether true has the value 1 or the value 0 for the bit, and this will not be

communicated outside your Bit class (that is, use private instance variables).

The following member methods and constructors must be implemented:

1. Bit

There will be two constructors for Bit objects. The ﬁrst constructor will have no

parameters and will construct a Bit object of the default value, and you will decide

whether 0 or 1 is the default. The second constructor will have a single int parameter

whose value is the value stored in the bit. (Actually, you will store true or false in the

data ﬁeld for the bit based on how you mapped true/false to 0/1.

2. set

This method makes the value stored in the Bit object equal to 1.

3. reset

This method makes the value stored in the Bit object equal to 0.

4. ﬂip

This method changes the value stored in the Bit object.

5. value

This method returns an int with a value of 0 or 1, based on the value stored in the Bit

object

Code :

public class Bit { private int bit; public boolean Bit() { bit = 0; } public Bit(int bit) { this.bit = bit; } public int set() { return bit = 1; } public int reset() { return bit = 0; } public int flip() { if (bit == 0) { return bit = 1; } else { return bit = 0; } } public int value() { return bit; } }

Here's the question for the second part, the register class.

The central processing unit of a computer uses registers to store information. A register

can be thought of as an array of bits. The operations we use in solving problems are converted

into operations on registers. In this project, you will be implementing register operations,

plus some other operations. In particular, you will write a class named Register. Register

will have an array, each slot of that array will be of type Bit. This will be the most important

data ﬁeld of a Register object, and that array will have private access. The following member

methods of Register will be implemented:

1. LSR

This is the logical shift right operation, it moves each bit one place to the right in the

register. The leftmost bit has a 0 placed into it.

2. LSL

This is the logical shift left operation. It moves each bit one place to the left in the

register. The rightmost bit has a 0 placed into it.

3. ASR

This is the arithmetic shift right operation. The only diﬀerence between this and LSR

is that the value in the leftmost bit remains unchanged by the operation, that is, it is

both shifted to the right and it is copied into itself.

4. ASL

This is the arithmetic shift left operation, and it has the exact same eﬀect as the LSL

operation.2

5. COMP

This is the complement operation, or the 1’s complement operation. It ﬂips every bit.

That is, if the bit has a value 1 it is changed to 0, and if it has a value 0 it is changed

to a value 1.

6. NEG

This is the negate operation. The eﬀect of this operation is to replace the contents of

the register with its 2’s complement value. The easiest way to explain this operation

is to take the 1’s complement, as in the COMP operation above, and then add 1. This

in fact negates the contents of the register.

7. ADD

This is an operation on 2 registers. Its eﬀect is to replace the contents of one of the

registers with the sum of the values in the two registers.

8. SUB

This is the subtract operation on two registers. It has the eﬀect of replacing the

contents of one register with the result of subtracting the other register’s contents

from its original contents. Subtraction is never performed directly. Instead, the register

to be subtracted is copied into another temporary register, the NEG operation is

performed on that temporary register, then the temporary register is added to register

to be subtracted from.

9. Register

This method is the Register constructor. Its purpose is to construct a Register. The

address, or reference, of that Register will be returned to the statement which called

the constructor. There will be two constructors, both of course named Register, but

with diﬀerent signatures. There will be a constructor with one parameter which is the

number of bits in the register. This constructor will create a Register object of the

correct size in which every bit will have the value 0. There will also be a constructor

with 2 parameters, the ﬁrst is the number of bits as above, the second will be the

value stored in the register. Note that this value must be converted to binary, and

the individual bits stored in the register. Note also that this value may be positive or

negative, but the NEG operation will be useful for any negative value.

Internally, your Register object will represent the data ﬁelds of the register as an array

of Bit objects. The Bit object will be discussed later in this document.

10. showRegister

This method will return a string whose value is the sequence of 1’s and 0’s stored in

the register, displayed in order from left to right.3

11. showValue

This method will return an int whose value is the integer value stored in the register.

12. size

This method returns the number of bits in the register as an int.

The problem I'm having with this code is that my array isn't working with the Bit class above. I'm pretty sure it's my lack of understanding of arrays and java in general but I'm hoping someone can shine a light on this problem for me. I also can't seem to make the methods I defined in the Bit class to work for the register class. Here's the code I have for the Register class.

Code :

public class Register { private static int i; private static int size; private static Bit[] bit = new Bit[size]; public static int LSR(Bit[] bit) { for (i = 0; i < bit.length; i++) { bit[i + 1] = bit[i]; bit[0].reset(); } } public static void LSL(Bit[] bit) { for (i = 0; i < bit.length; i++) { bit[i] = bit[i + 1]; bit[bit.length].reset(); } bit[bit.length].reset(); } public static void ASR(Bit[] bit) { for (i = 0; i < bit.length; i++) { bit[i + 1] = bit[i]; } } public static void ASL(Bit[] bit) { for (i = 0; i < bit.length - 1; i++) { bit[i] = bit[i + 1]; } } public static void COMP(Bit[] bit) { for (i = 0; i < bit.length; i++) { bit[i].flip(); } } public static void NEG(Bit[] bit) { bit = COMP(bit); for (i = bit.length; i > 0; i--) { if (bit[i].equals(0)) { bit[i].set(); i = 0; } else { bit[i].reset(); } } } public static void ADD(Bit[] bit,Bit[] bit2) { for (i = bit.length; i > 0; i--) { if ((bit[i].equals(bit2[i])) && (bit[i].equals(1))) { bit[i].reset(); bit[i-1].flip(); } else if ((bit[i].equals(bit2[i])) && (bit[i].equals(0))) { bit[i].reset(); } else { bit[i].set(); } } } public static void SUB(Bit[] bit, Bit[] bit2) { bit2 = NEG(bit2); for (i = bit.length; i > 0; i--) { if ((bit[i].equals(bit2[i])) && (bit[i].equals(1))) { bit[i].reset(); bit[i-1].flip(); } else if ((bit[i].equals(bit2[i])) && (bit[i].equals(0))) { bit[i].reset(); } else { bit[i].set(); } } } } public static void Register(int size) { this.size = size; } public static void Register(int size, int number) { this.size = size; } public static void showRegister(Bit[] bit) { for (i = 0; i < bit.length; i++) { } } public static void showValue(Bit[] bit1) { for (i = 0; i < bit.length; i++) { } } public static int size() { return bit.length; } }