Two 32 bit integer values A and B, are processed to give the 32 bit integers C and D as per the following rules. Which of the rule(s) is(are) reversible? i.e. is it possible to obtain A and B given c and D in all condition?
A. C = (int32)(A+B), D = (int32)(A-B)
B. C = (int32)(A+B), D= (int32)((A-B)>>1)
C. C = (int32)(A+B), D = B
D. C = (int32)(A+B), D = (int32)(A+2*B)
E. C = (int32)(A*B), D = (int32)(A/B)
A few questions about the integer arithmetic. Modular addition forms amathematical structure known as an abelian group. How about signed addition? It's also commutative (that’s where the “abelian” part comes in) and associative, is this forms a n an abelian group?
Given that integer addition is commutative and associative, C is apparently true, because we can retrieve A by (A+(B-B)). What about D? Can we assume that 2 * B = B + B st. B = A+B+B-(A+B)?
And multiplication is more complicated, but I know that it can not be retrieve A if there is an overflow.
unsignedinteger types form groups with respect to addition in C and C++. Signed addition usually can't, already for the simple reason thatINT_MINusually has no inverse in the common 2complement representation.INT32_MINforAandBand it is implementation defined what will happen in any case, so there is no answer. I get the suspicion that you are trying to get a consistent answer to an ill-posed exercise. Often we see such exercise here that come from not-so-well-informed teachers that take two's complement for granted and never heard of signals that can be raised in case of overflow. Don't do such stuff withint32(BTW the standardtypedefis calledint32_t) but withuint32_t. Where do you have these questions from?E = C - D = A + B - B = Aand B byC - E. What do you think? And I take D as a right answer, see my questions.