JavaRippleAdder/longword.java
JavaRippleAdder/longword.java
public class longword implements ILongword{
public bit bits[] = new bit[32]; // bit[] of size 32 is created
public longword()
{
for(int i=0;i<32;++i) this.bits[i]=new bit();
}
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.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<32;++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<32;++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<32;++i)
{
l.setBit(i,this.bits[i].xor(other.bits[i]));
}
return l;
}
public longword not()
{
longword l = new longword();
for(int i=0;i<32;++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==32) return l;
for(int k=amount;k<32;++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<32-amount;++j) l.setBit(i++,this.bits[j]);
return l;
}
public String toString()
{
StringBuffer s = new StringBuffer();
boolean newComma=false;
for(int i=0;i<32;++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<32;++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
{
int num=0;
for(int i=0;i<31;++i)
{
if(this.bits[i].getValue()==1) num+=(int)Math.pow(2,i);
}
if(this.bits[31].getValue()==1) return num*=-1;
return num;
}
public void copy(longword other)
{
if(other==null) throw new IllegalArgumentException("other object is null");
for(int i=0;i<32;++i) this.bits[i].set(other.bits[i].getValue());
}
public void set(int value)
{
if(value<0) value*=-1; // will not consider negative values in testing
int i=value;
int j=0;
bit b;
while(i>0)
{
b=new bit();
b.set(i%2);
this.setBit(j,b);
++j;
i/=2;
}
}
}
JavaRippleAdder/bit_test.java
JavaRippleAdder/bit_test.java
import java.util.*;
public class bit_test
{
public static void runTests()
{
bit b1,b2;
b1=new bit();
b2=new bit();
b1.set(0);
b2.set(1);
System.out.println("b1: "+b1);
System.out.println("b2: "+b2);
b1.clear();
System.out.println(b1);
b2.set();
System.out.println(b2);
System.out.println("and:"+b1.and(b2));
System.out.println("or:"+...