In the tfhe-rs hand book we are getting ‘Detecting Overflows’ (in 3.14) . I can’t under stand how it work ? and also what is carry? Basically I want under stand how actually comparison operation work?
please clear all things.
When you add two digits, the result may not fit, and so it will generate a carry, the carry is meant to be added to the next digit:
e.g. in base 10, with just one digit 9 + 1 = 0, and carry 1
in base 2, with 8 digits (u8) 255 + 1 = 0u8, and carry 1
Overflow if when the result of the operation does not fit, e…g. in base 10, with just one digit 9 + 1 overflows because to represent 10 we would need two digits
For unsigned integers If the last digits generates a carry (or a borrow when subtracting) it means the result overflows, for signed integers however as we use twos complement representation, the carry cannot be used to know if there is an overflow.
The comparison a < b works by computing a - b, for unsigned integers if a < b, then a - b overflows, and the last digit generates a borrow, so we check for that
For signed integers we also need to compute whether a - b overflows, but for that we can’t just look at the borrow, and we also have to combine the overflow information with the value of the sign bit of a - b