-
bit array and register problem
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 field 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 first 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 field 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. flip
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 field 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 difference 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 effect as the LSL
operation.2
5. COMP
This is the complement operation, or the 1’s complement operation. It flips 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 effect 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 effect 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 effect 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 different 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 first 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 fields 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;
}
}
-
Re: bit array and register problem
Does your code even compile? It looks as though it should require a no-args constructor for the array declaration, and you seem to have messed up the no-args constructor in your Bit class, instead creating a method called Bit() that would return a boolean. There's an example of a no-argument constructor near the top of this page:
Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
You seem also to have not followed the instruction:
Quote:
you will use boolean for the data type
-
Re: bit array and register problem
Please edit your post and remove the code tags from around the text. The folding of the lines makes it hard to read.
Quote:
The problem I'm having with this code is that my array isn't working with the Bit class above
Can you explain what "isn't working" means.
You get errors.
The results are wrong. If so please show what the results are and explain what they should be.
-
Re: bit array and register problem
sean4u:
I don't really understand what the boolean data type means. Does my professor want the output to be true/false or just 0/1?
norm:
The register class isn't compiling at all. I'm getting problems like incompatible type and cannot find symbol. I keep confusing how to use my Bit() class and int().
-
Re: bit array and register problem
Quote:
I'm getting problems
Please copy and paste here the full text of the error messages.
-
Re: bit array and register problem
Quote:
I don't really understand what the boolean data type means
It's a 'primitive data type':
Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
-
Re: bit array and register problem
----jGRASP exec: javac -g Register.java
Register.java:11: cannot find symbol
symbol : method reset()
location: class Bit[]
bit[0] = bit.reset();
^
Register.java:21: incompatible types
found : int
required: Bit
bit[bit.length] = 0;
^
Register.java:41: incompatible types
found : int
required: Bit
bit[i] = bit[i].flip();
^
Register.java:46: COMP(Bit[]) in Register cannot be applied to (Bit)
bit[i] = COMP(bit[i]);
^
Register.java:57: cannot find symbol
symbol : variable bit1
location: class Register
Bit[] bit3 = bit1[i] + bit2[i];
^
Register.java:57: cannot find symbol
symbol : variable bit2
location: class Register
Bit[] bit3 = bit1[i] + bit2[i];
^
Register.java:57: incompatible types
found : java.lang.String
required: Bit[]
Bit[] bit3 = bit1[i] + bit2[i];
^
Register.java:65: operator - cannot be applied to Bit,Bit
Bit[] bit3 = bit2[i] - bit1[i];
^
8 errors
-
Re: bit array and register problem
Quote:
bit[0] = bit.reset();
The variable bit is an array. Arrays do not have a method reset().
What are you trying to do with that statement?
Quote:
bit[bit.length] = 0;
What does the bit array contain? You can only assign values to elements in the array with the data that they are supposed to contain. 0 is an int not a Bit object.
Most of these errors are of the same type. You are mixing apples and oranges.
-
Re: bit array and register problem
I defined the reset() method in the class Bit() to set the value of the bit to 0.
-
Re: bit array and register problem
What is the variable: bit?
Is it a Bit object?
or is it an array of Bit objects?
You need a Bit object to call the Bit class's methods.
To get a Bit object from an array of Bit objects you'd code something like this: bit[ix]
-
Re: bit array and register problem
I had the line
private static Bit[] bit = new Bit[size];
to create a Bit[] with the variable name bit with an array of Bit objects. I want to set the first bit ( bit[0] ) equal to zero, which is what I defined my reset() method is supposed to do.
-
Re: bit array and register problem
The code you posted did NOT use bit[0] as the object reference. It was used as a reference to the array.
Go back and see how you coded it.
You need to get a reference to the Bit object and call its method. Something like this:
refToBitObject.itsMethod()
-
Re: bit array and register problem
I hate asking stupid questions but I just don't seem to understand.
bit[0] refers to the bit[] array but isn't the bit[] array an array of the Bit object? Doesn't that mean that the bit[0] is also a Bit object?
Or do I have to do something like this:
private Bit bitVariableName = new Bit();
So that bitVariableName refers to the Bit object. Though if I do this, then bitVariableName is no longer an array.
-
Re: bit array and register problem
Yes bit[0] is a Bit object and you should use that to call the Bit class's methods.
bit is NOT a Bit object and you can NOT use that to call the Bit class's methods
You coded: bit.reset();
You need to code: bit[0].reset();
-
Re: bit array and register problem
okay that makes sense. So now I have this
Code :
public static void LSR(Bit[] bit)
{
for (i = 0; i < bit.length; i++)
{
bit[i + 1] = bit[i];
bit[0] = bit[0].reset();
}
}
but I'm getting the error:
Register.java:11: incompatible types
found : void
required: Bit
bit[0] = bit[0].reset();
What does it mean by the "required: Bit" the bit[0] should still be a Bit object.
-
Re: bit array and register problem
What type does the bit reset() method return? The compiler sees that the reset method is void.
You are assigning what that method returns to an element in the bit array which can only be assigned Bit objects.
Think about what an assignment statement does.
The right hand of the = must evaluate to or return a value.
That value is assigned to the variable to the left of the =
-
Re: bit array and register problem
My reset method is currently void. Should I change it to Bit?
If I have a code like this
Code :
public int reset()
{
return bit = 0;
}
then the method will return an integer. If I want it to return a Bit, should I do something like this?
Code :
public Bit reset()
{
return bit = bit.Bit(0);
}
-
Re: bit array and register problem
BEFORE you write the code for a method you need to define what the method is to do.
What is the purpose of the reset method?
Does it create a new Bit object that you want to save in the bit array?
Or does it change the contents of the Bit object it is in?
Or what ?
-
Re: bit array and register problem
the reset method is supposed to change the contents of the Bit object. It is supposed to change the value of the content to 0. That's why I originally though to use int object.
-
Re: bit array and register problem
If the reset method does NOT return anything then you should not be assigning it to a slot in the array:
bit[0] = bit.reset(); // assigns what reset returns to a slot in the array
If you want to reset the element at 0 code:
bit[0].reset();
-
Re: bit array and register problem
Okay I think I fixed most of those errors. Now I have to create a Register method. What the professor wants is this:
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 different 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 first 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 fields of the register as an array
of Bit objects. The Bit object will be discussed later in this document.
If I interpret this correctly, I think he wants something like this:
Code :
public Register(int numberOfBits)
{
Bit[] bit = new Bit[numberOfBits];
}
public Register(int numberOfBits, int valueStored)
{
Bit[] registerBit = new Bit[numberOfBits];
//code to parse valueStored from int to binary and store it in registerBit[]?
}
but when I try to compile that, I get this error:
----jGRASP exec: javac -g Register.java
Register.java:101: class, interface, or enum expected
public static void Register(int numberOfBits)
^
Register.java:104: class, interface, or enum expected
}
^
Register.java:105: class, interface, or enum expected
public static void Register(int numberOfBits, int valueStored)
^
Register.java:110: class, interface, or enum expected
}
-
Re: bit array and register problem
What code is at the lines in the error messages?
It sounds like a mismatched pair of {}s. Check that all {s have a matching } where it should be.
Also check for matching pairs of [] and ().
Why is the posted text formatted with so many short lines?
You could take the time to make the text easier to read by removing all the extra line ends so there are not so many short lines.
-
Re: bit array and register problem
I found it. There was an extra } before this statement.
-
Re: bit array and register problem
That's the most common cause.
Another is code statements outside of a method.
-
Re: bit array and register problem
for
Code :
public Register(int numberOfBits)
{
Bit[] bit = new Bit[numberOfBits];
}
I'm supposed to set the array to all values of 0. I thought I did this when I did:
Code :
public Bit()
{
bit = 0;
}
however, I'm getting null values for the entire array. Is the Bit() method not doing what I think I'm telling it to do.