I'm trying to prove the correctness of the following function and I'm having trouble determining the loop invariant:
function add(y, z) //returns y + z
x = 0; c = 0; d = 1;
while (y > 0) or (z > 0) or (c > 0) doa = y % 2;
b = z % 2;
if a xor b xor c then x = x + d;
c = (a and b) or (b and c) or (a and c);
d = 2d; y = ⌊y/2⌋; z = ⌊z/2⌋;return(x)
On my own, I thought (a and b = c) is the loop invariant, because it was true for every iteration in the set of values of y and z that I've tested. I'm not really sure how that factoid is relevant in proving the correctness of the algorithm.