QB64 Phoenix Edition
Adding without adding - Printable Version

+- QB64 Phoenix Edition (https://staging.qb64phoenix.com)
+-- Forum: QB64 Rising (https://staging.qb64phoenix.com/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://staging.qb64phoenix.com/forumdisplay.php?fid=3)
+---- Forum: Programs (https://staging.qb64phoenix.com/forumdisplay.php?fid=7)
+---- Thread: Adding without adding (/showthread.php?tid=392)



Adding without adding - SMcNeill - 05-11-2022

So, how do you add two numbers, without adding them?  

Code: (Select All)
Print Add(200, 22, Overflow~%%);: If Overflow~%% Then Print "Overflow!" Else Print
Print Add(245, 22, Overflow~%%);: If Overflow~%% Then Print "Overflow!" Else Print

Function Add (A As _Unsigned _Byte, B As _Unsigned _Byte, Overflow As _Unsigned _Byte)
    Dim As _Unsigned _Byte CarryFlag
    Overflow = 0
    While B <> 0
        CarryFlag = A And B
        Overflow = Overflow Or _SHR(CarryFlag, 7)
        A = A Xor B
        B = _SHL(CarryFlag, 1)
    Wend
    Add = A
End Function

You XOR, Bit shift, and AND them into submission!   Big Grin