Advertisement
Guest User

UnsignedInt

a guest
Nov 29th, 2015
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1. class UnsignedInt{
  2.     private long value;
  3.     private static final long MODULO = (long)Math.pow(2, 32);
  4.  
  5.     public UnsignedInt(String value){
  6.         this.value = Long.valueOf(value);
  7.         toUnsigned();
  8.     }
  9.  
  10.     public UnsignedInt(long value) {
  11.         this.value = value;
  12.         toUnsigned();
  13.     }
  14.  
  15.     private void toUnsigned(){
  16.         if (value < 0)
  17.             value = value % MODULO == 0 ? 0 : value + (-value / MODULO + 1) * MODULO;
  18.         else
  19.             value = value % MODULO;
  20.     }
  21.  
  22.     private long getValue(){
  23.         return this.value;
  24.     }
  25.  
  26.     public void add(UnsignedInt value) {
  27.         this.value = (this.value + value.getValue()) % MODULO;
  28.     }
  29.  
  30.     public void subtract(UnsignedInt value) {
  31.         this.value = (this.value - value.getValue() + MODULO) % MODULO;
  32.     }
  33.  
  34.     @Override
  35.     public String toString() {
  36.         return String.valueOf(value);
  37.     }
  38.  
  39.     public void multiply(UnsignedInt value) {
  40.         this.value = this.value * value.getValue();
  41.         toUnsigned();
  42.     }
  43.  
  44.     public void divide(UnsignedInt value) {
  45.         this.value = this.value / value.getValue();
  46.         toUnsigned();
  47.     }
  48.  
  49.     public void modulo(UnsignedInt value) {
  50.         this.value = this.value % value.getValue();
  51.         toUnsigned();
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement