Please do not suggest using any gems or creating classes for unsigned integers.
Input data - strings that represent 4 byte sequence. Output data should be the same type of string.
This is what I've got so far:
def addition(augend, addend)
sum = augend.to_i(16)+addend.to_i(16)
sum -= 2**32 if sum >= 2**32
sum = "%08X" % sum
return(sum)
end
def subtraction(minuend, subtrahend)
difference = 0
# Some magic here.
difference = "%08X" % difference
return(difference)
end
puts addition('FFFFFFFF', '00000002') # 00000001
puts addition('B1B0AFBA', 'FEEE302D') # B09EDFE7
puts subtraction('00000055', '00000005') # 00000000, expected 00000050
puts subtraction('B1B0AFBA', 'FEEE302D') # 00000000, expected B2C27F8D
How should subtraction()
be implemented? And could addition()
be improved?