src/main/java/com/osiris/maths/binary/Bits.java
/**
     * Adds the provided number to the current one.
     * @return the current object for chaining.
     */
Adds the provided number to the current one.
@return the current object for chaining.
Adds the provided number to the current one . @return the current object for chaining .
public Bits add(Bits num, boolean printSteps) {
        if (this.bits.size() != num.bits.size()) {
            int missingBits = 0;
            if (bits.size() < num.bits.size()) {
                missingBits = num.bits.size() - bits.size();
                for (int i = 0; i < missingBits; i++) {
                    if(state == State.COMPLEMENT_1 || state == State.COMPLEMENT_2) bits.add(new Bit(true));
                    else bits.add(new Bit(false));
                }
            } else {
                missingBits = bits.size() - num.bits.size();
                for (int i = 0; i < missingBits; i++) {
                    if(state == State.COMPLEMENT_1 || state == State.COMPLEMENT_2) num.bits.add(new Bit(true));
                    else num.bits.add(new Bit(false));
                }
            }
        }
        if(printSteps){
            System.out.println("Addition:");
            System.out.println(num);
            System.out.println("+");
            System.out.println(this);
            System.out.println("=");
        }
        boolean lastOverflow = false;
        boolean nextOverflow = false;
        for (int i = 0; i < this.bits.size(); i++) {
            boolean b0 = this.bits.get(i).value;
            boolean b1 = num.bits.get(i).value;
            if (!b0 && !b1) { 
                if(printSteps) System.out.println("[" + i + "] 0+0=0");
                this.bits.set(i, new Bit(false));
            } else if (b0 && b1) { 
                if(printSteps) System.out.println("[" + i + "] 1+1=0 overflow:1");
                this.bits.set(i, new Bit(false));
                nextOverflow = true;
            } else { 
                if(printSteps) System.out.println("[" + i + "] 1+0 or 0+1 =1");
                this.bits.set(i, new Bit(true));
            }
            if (lastOverflow) {
                if (this.bits.get(i).value) { 
                    if(printSteps) System.out.println("[" + i + "/overflow] 1+1=0 overflow:1");
                    this.bits.set(i, new Bit(false));
                    nextOverflow = true;
                } else { 
                    if(printSteps) System.out.println("[" + i + "/overflow] 1+0 or 0+1 =1");
                    this.bits.set(i, new Bit(true));
                }
            }
            lastOverflow = nextOverflow;
            this.overflow.value = lastOverflow;
            nextOverflow = false; 
        }
        if(printSteps) System.out.println("Result: "+this);
        return this;
    }
public Bits add ( Bits num , boolean printSteps ) { if ( this . bits . size ( ) != num . bits . size ( ) ) { int missingBits = 0 ; if ( bits . size ( ) < num . bits . size ( ) ) { missingBits = num . bits . size ( ) - bits . size ( ) ; for ( int i = 0 ; i < missingBits ; i ++ ) { if ( state == State . COMPLEMENT_1 || state == State . COMPLEMENT_2 ) bits . add ( new Bit ( true ) ) ; else bits . add ( new Bit ( false ) ) ; } } else { missingBits = bits . size ( ) - num . bits . size ( ) ; for ( int i = 0 ; i < missingBits ; i ++ ) { if ( state == State . COMPLEMENT_1 || state == State . COMPLEMENT_2 ) num . bits . add ( new Bit ( true ) ) ; else num . bits . add ( new Bit ( false ) ) ; } } } if ( printSteps ) { System . out . println ( "Addition:" ) ; System . out . println ( num ) ; System . out . println ( "+" ) ; System . out . println ( this ) ; System . out . println ( "=" ) ; } boolean lastOverflow = false ; boolean nextOverflow = false ; for ( int i = 0 ; i < this . bits . size ( ) ; i ++ ) { boolean b0 = this . bits . get ( i ) . value ; boolean b1 = num . bits . get ( i ) . value ; if ( ! b0 && ! b1 ) { if ( printSteps ) System . out . println ( "[" + i + "] 0+0=0" ) ; this . bits . set ( i , new Bit ( false ) ) ; } else if ( b0 && b1 ) { if ( printSteps ) System . out . println ( "[" + i + "] 1+1=0 overflow:1" ) ; this . bits . set ( i , new Bit ( false ) ) ; nextOverflow = true ; } else { if ( printSteps ) System . out . println ( "[" + i + "] 1+0 or 0+1 =1" ) ; this . bits . set ( i , new Bit ( true ) ) ; } if ( lastOverflow ) { if ( this . bits . get ( i ) . value ) { if ( printSteps ) System . out . println ( "[" + i + "/overflow] 1+1=0 overflow:1" ) ; this . bits . set ( i , new Bit ( false ) ) ; nextOverflow = true ; } else { if ( printSteps ) System . out . println ( "[" + i + "/overflow] 1+0 or 0+1 =1" ) ; this . bits . set ( i , new Bit ( true ) ) ; } } lastOverflow = nextOverflow ; this . overflow . value = lastOverflow ; nextOverflow = false ; } if ( printSteps ) System . out . println ( "Result: " + this ) ; return this ; }
Adds the provided number to the current one.
Adds the provided number to the current one .
// Fill missing bits of smaller list // 0 0 // 1 1 // 0 1 or 1 0 // Add last overflow to result if existing // 1 1 // 0 1 or 1 0 // Reset