JavaLongWord/longword.java
JavaLongWord/longword.java
public class longword implements ILongword{
public bit bits[] = new bit[32]; // bit[] of size 32 is created
public int size;
public longword()
{
for(int i=0;i<32;++i) this.bits[i]=new bit();
this.size=0;
}
public bit getBit(int i) // Get bit i
{
if(i>=32) throw new IllegalArgumentException("index out of bound");
return this.bits[i];
}
public void setBit(int i, bit value) // set bit i's value
{
if(value==null) throw new IllegalArgumentException("value is null");
if(i>=32)
{
throw new IllegalArgumentException("index out of bounds. longword can be of size 32");
}
this.size++;
this.bits[i].set(value.getValue());
}
public longword and(longword other) // and two longwords, returning a third
{
if(other==null) throw new IllegalArgumentException("object is null");
longword l = new longword();
for(int i=0;i
{
l.setBit(i,this.bits[i].and(other.bits[i]));
}
return l;
}
public longword or(longword other) // or two longwords, returning a third
{
if(other==null) throw new IllegalArgumentException("object is null");
longword l = new longword();
for(int i=0;i
{
l.setBit(i,this.bits[i].or(other.bits[i]));
}
return l;
}
public longword xor(longword other) // xor two longwords, returning a third
{
if(other==null) throw new IllegalArgumentException("object is null");
longword l = new longword();
for(int i=0;i
{
l.setBit(i,this.bits[i].xor(other.bits[i]));
}
return l;
}
public longword not() // not two longwords, returning a third
{
longword l = new longword();
for(int i=0;i
{
l.setBit(i,this.bits[i].not());
}
return l;
}
public longword rightShift(int amount) // rightshift this longword by amount Bits, creating a new longword
{
if(amount>=32 || amount<=0) throw new IllegalArgumentException("amount should be between 0 and 31");
int i=0;
longword l = new longword();
if(amount>=this.size)
{
l.set(0);
}
else
{
for(int k=amount;k
{
l.setBit(i,this.bits[k]);
i++;
}
}
return l;
}
public longword leftShift(int amount)// leftshift this longword by amount Bits, creating a new longword
{
if(amount>=32 || amount<=0) throw new IllegalArgumentException("amount should be between 0 and 31");
longword l=new longword();
int i;
for(i=0;i
for(int j=0;j
return l;
}
public String toString()
{
StringBuffer s = new StringBuffer();
boolean newComma=false;
if(this.size==0)
{
s.append("0");
}
else
{
for(int i=0;i
{
if(newComma) s.append(",");
s.append(String.valueOf(this.bits[i].getValue()));
newComma=true;
}
}
return s.toString();
}
public long getUnsigned() // returns the value of this longword as a long
{
long num=0;
for(int i=0;i
{
if(this.bits[i].getValue()==1) num+=(int)Math.pow(2,i);
}
return num;
}
public int getSigned() // returns the value of this longword as an int