Question: For the machine that we are simulating, we will be using a 32-bit value for both addresses and values. A word is a collection of
For the machine that we are simulating, we will be using a 32-bit value for both addresses and values. A word is a collection of bits, originally the size that the machine natively works on. Along the history of computing, a word became 16 bits. A longword is a 32-bit collection of bits.
You must fully implement this interface. You must make a new class called Longword that does not inherit from anything. You must create a collection (array is best) of Bit (from class bit) and use that for storage. You may not use any other storage mechanism.
public interface ILongword {
bit getBit(int i); // Get bit i
void setBit(int i, bit value); // set bit i's value
longword and(longword other); // and two longwords, returning a third
longword or(longword other); // or two longwords, returning a third
longword xor(longword other);// xor two longwords, returning a third
longword not(); // negate this longword, creating another
longword rightShift(int amount); // rightshift this longword by amount bits, creating a new longword
longword leftShift(int amount);// leftshift this longword by amount bits, creating a new longword
@Override
String toString(); // returns a comma separated string of 0's and 1's: "0,0,0,0,0 (etcetera)" for example
long getUnsigned(); // returns the value of this longword as a long
int getSigned(); // returns the value of this longword as an int
void copy(longword other); // copies the values of the bits from another longword into this one
void set(int value); // set the value of the bits of this longword (used for tests)
}
you may use loops to implement the same operation done on each bit. You must use the operations from bit (and, or, not, xor, getBit, set) where appropriate. You must validate inputs where appropriate.
public class bit { private int value; // constructor to initialize the bit value to 0 public bit() { value = 0; }
// method to set the bit's value to passed value @Override public void set(int value) { if(value >= 0 && value <= 1) // validate input value is 0 or 1 this.value = value; else // invalid value, throw exception throw new IllegalArgumentException("ERROR: Bit can be set to 0 or 1"); }
// method to toggle the value from 0 to 1 or 1 to 0 @Override public void toggle() { if(value == 0) value = 1; else value = 0; }
// method to set value to 1 @Override public void set() { value = 1; }
// method to set value to 0 @Override public void clear() { value = 0; }
// method to return bit's value @Override public int getValue() { return value; }
// method to perform and on two bits and return a new bit set to the result @Override public bit and(bit other) { bit result = new bit(); // create a new bit for the result if(value == 0) // if any one of the value is 0, set result to 0 result.clear(); else { if(other.getValue() == 0) result.clear(); else // if both values are 1 , set result to 1 result.set(); } return result; }
// method to perform or on two bits and return a new bit set to the result @Override public bit or(bit other) { bit result = new bit(); // create a new bit for the result if(value == 1) // if any one of the value is 1, set the result to 1 result.set(); else { if(other.getValue() == 1) result.set(); else // both values are 0, set result to 0 result.clear(); } return result; }
// method to perform xor on two bits and return a new bit set to the result @Override public bit xor(bit other) { bit result = new bit(); // create a new bit for the result if(value == 1) // if one of the bit's value is 1, set result to 1, else set it to 0 { if(other.getValue() == 0) result.set(); else result.clear(); }else { if(other.getValue() == 1) result.set(); else result.clear(); } return result; }
// method to perform not on the existing bit, returning the result as a new bit @Override public bit not() { if(value == 0) set(); else clear(); return this; } // method to return String representation of the bit public String toString() { return value+""; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
