; This is an implementation of the ripple carry adder ; http://en.wikipedia.org/wiki/Adder_(electronics)#Ripple_carry_adder #include #include ; using strings for simple user input Dim $numA = "00001001" ; 9 Dim $numB = "11111100" ; -4 ; Try a big value, such as 11111111 ; And add a value 1 to it, so overflow will occur ; $numA = "11111111" ; $numB = "00000001" ; Gets the longest string of the two and turns them into a binary array like: [1,0,1,1] Dim $len = _LongestString($numA,$numB) ; Alternatively you can set this to a number like 8 to indicate the number of full adders to use $len = 8 ; 16 bits full adder Dim $binA = _BinStringToBinArray($numA, $len) Dim $binB = _BinStringToBinArray($numB, $len) ; Makes a variable for the result number Dim $sum[$len] Dim $carry = 0 For $i = 0 to $len-1 $res = _FullAdder( $binA[$i], $binB[$i], $carry ) $sum[$i] = $res[0] $carry = $res[1] Next If ( $carry == 1 ) Then ; Overflow MsgBox(0,"AutoIt Implementation of Full Adder","Overflow has occured, your results will not be accurate.") EndIf MsgBox(0, "AutoIt Implementation of Full Adder", "Result: " & _BinArrayToBinString($sum) ) ;; Utility function for IO ; Turns a string "1011" into a 'binary array' : [1,0,1,1] Func _BinStringToBinArray($in, $len = 8) $in = _StringReverse($in) ; it would've been easier to switch the MSB and LSB but whatever Local $t = StringSplit($in,"") Local $ret[$len] For $i = 0 to $len-1 $ret[$i] = 0 Next For $i = 1 to UBound($t)-1 $ret[$i-1] = Number($t[$i]) Next Return $ret EndFunc ; Turns a string "1011" into a 'binary array' : [1,0,1,1] Func _BinArrayToBinString($in) Local $ret = "" For $i = UBound($in)-1 to 0 Step -1 $ret &= String($in[$i]) Next Return $ret EndFunc ; Returns the length of the longest string Func _LongestString($a,$b) Return _Max(StringLen($a),StringLen($b)) EndFunc ;; Binary mathematical functions ; http://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder Func _FullAdder($a, $b, $cIn = 0) Local $cOut = BitOR( BitAND($a,$b) , BitAND($cIn, BitXOR($a,$b))) Local $S = BitXOR(BitXOR($a,$b),$cIn) local $ret[2] = [$S,$cOut] Return $ret EndFunc ; http://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder Func _HalfAdder($a,$b) Local $S = BitXOR($a,$b) ; sum Local $C = BitAND($a,$b) ; carry local $ret[2] = [$S,$C] Return $ret EndFunc