# One's Complement Math

How todo 8 bit one’s complement addition with carry added back to the result?

Not sure exactly what you’re trying to do. Could you provide an example?

not sure if this is right as its been 40+ years since I cared about 1’s or 2’s complement values

``````Private Function one_comp(x as int8, y as int8) as int16
dim z as int16
if x<0 then x=not x
if y<0 then y=not y
z=x+y
if z>&HFF then z=z+1 // carry
z=z and &HFF
return z

End Function``````

Thanks Dave.
I will give it a try!

Robert: I have this checksum calculation i need this kind of math in.
My original function works in 99 % of the messages, great to see a different approach.

```// return the "one's complement sum" of n 8 bit values Function OnesCompSum(paramArray uInts As UInt8) As UInt8 Dim n As UInt16 = 0 For Each x As UInt8 in uInts n = n + x // unsigned sum If n>&h0FF Then n = (n+1) And &h0FF // Add carry back and keep it 8 bit Next n = (not n) And &h0FF // one's complement is done just after the final sum Return n End Function ```

Thanks Rick, even better! Will fit right in my checksum calculation routine.