Two’s complement binary arithmetic

arithmeticbinarydiscrete mathematicselementary-number-theorynumber theory

Suppose binary values are signed 8-bit values, representing twos-complement format with a decimal range from -128 to 127. Which of the following statements are true/false?

1) 00001010 > 00000111 

2) 11111111 > 01111111

3) (11111111 + 11111111) > (00000001 - 00000010)

4) (00000100 * 00000100) == 00010000

5) (11111010 * 00000011) == (11101110)

6) (10000000 / 00000100) == 11100000

7) 11110000 - 00000001 == 10001111 

I'm studying for my exam and I'm struggling with this problem. Here's what I've got for each of them.

1) In decimal, the LHS is 10 and the RHS is 7. So it is true.

2) In decimal, the LHS is -1, and the RHS is 127. So it's false.

3) I'm really not sure about this one. I know that overflown digits get dropped, but I don't know if there's an overflow here. Here's my guess: The LHS is (-1 + -1) in decimal and the RHS is -1. So my answer is false

4) I think this is true. I don't think anything special is used here.

5) The LHS is (-6 * 3) and the RHS is -18. So it's true?

6) The LHS is -128 / 4 and the RHS is -32, so it's true

7) The LHS is -16 – 1 and the RHS i got -113. So I think it's false?


Can someone please let me know if these are ok?

Thanks

Best Answer

Your answers are correct, but I am concerned that you keep having to translate to decimal to reach your conclusions. Here I will demonstrate how I would reason about these questions directly in binary, which you may find useful:

1) Leftmost bits both zero, hence both positive. Leftmost 1 in left number is bit 4, in right number is bit 3; hence left is greater than right. Conclusion: TRUE.

2) Leftmost bit in left number is 1, hence negative; in right number is 0, hence positive. Negative is -not- greater than positive. Conclusion: FALSE.

3) Subtraction in right expression calculated by taking bit complement and adding 1, then adding terms. Result is [sequence of 7 0's followed by 1] plus [sequence of 7 1's followed by 0]. Sum calculable without carry, result is sequence of 8 1's, a negative number. This matches leftmost term in left expression, to which another negative number is added, hence left expression must be smaller. Conclusion: FALSE.

4) Leftmost bits both zero, hence both positive. Leftmost 1 bits in terms of left expression followed by two 0's, leftmost 1 bit in right term followed by four 0's. In product of two terms -carried out in usual on-paper procedure-, result will be eight staggered 8-bit values, of which the third from the top is the first term shifted left by two and the rest are all 0's, hence total sum will be first term shifted by two, which 'inserts two 0's at right', for total of 4. Conclusion: TRUE.

5) [here very tempting to use decimal, but...] Negate the two negative values [note that this leaves the left-right relationship unchanged]; the product on the left is then much simpler, and easy to compare to the right value. Conclusion: TRUE.

6) Multiply both sides by the second term on the left (thereby 'moving the term to the other side'). Second term has a single 1 in bit position 3; hence multiplication 'shift' value on the right two bits left. Cutting out the rightmost 8 bits of the result yields the first term on the left. Conclusion: TRUE.

7) Second term on left consists of a single 1 in bit position 1. Subtraction of 1, quick rule: Flip all 0's to 1's moving right-to-left, when a 1 reached flip it to 0. Doing this with the first term on the left yields three 1's followed by 0 followed by four 1's, which does NOT match the right side. Conclusion: FALSE.