October 3rd, 2012, 12:32 AM
Cute stuff. HINTS:
1. Print out the results of (neg_x & sign) and (x & ~sign) separately. Then convert them into binary and you'll get an idea of what the | does (it is pretty easy once you see the two results in binary to figure out what is going on)
2. The reason why this works is because initially the numbers all occupy one byte of (an assumed 32 bit) int. i.e. the numbers are of the format 000a, 000b, 000c, 000d. By bit shifting the bits, it essentially makes two variables
X = 00ab
Y = 00cd
where each variable occupies one byte of the int (this is why there is a restriction that a, b, c and d are <= 127) Again, printing x, y in binary along with a and n will tell you exactly what is happening
Now it adds the numbers, which will add the numbers on individual bytes like this:
Q = 00(a+c)(b+d)
Again, if you print Q in binary, you'll see what is going on. Also note here the restriction that 0 <= a, b, c, d <= 127. Because if the result of (b+d) is > 255, then it will overflow into the calculation of (a+c). Overflows cannot happen if both numbers are <= 127.
The final step is to separate byte (b+d) and byte (a+c) and then add them together to get the final result.
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne