Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding without adding
#1
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
Reply




Users browsing this thread: 1 Guest(s)