0

For the addition operations below, find the result that would be given by a computer using 8-bit, 2's complement numbers:

-93 + -35

45 + 114

My answer:

First problem: Binary representation of 128= 10000000

-128 (2's compliment of 128) = 10000000

Second problem:

45 in 2's complement= 00101101

114 in 2's compliment=01101000

45+114 = 10010101

Am I correct? If not, could someone please show me how to achieve the right solution for both?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • I cannot tell if it is correct because behavior on overflow is not determined just by "using 8-bit, 2's complement numbers". – MikeCAT Jun 25 '21 at 22:58
  • Also it may be wrong to give results in binary while the input is given in decimal (at least not binary), though the output format is not specified. – MikeCAT Jun 25 '21 at 22:59
  • One more point is that a computer using 8-bit, 2's complement numbers may be able to emulate other format of numbers. (a turing-complete machine can) – MikeCAT Jun 25 '21 at 23:00
  • The first is likely what the question author is expecting. The second is not. Show your work. – Eric Postpischil Jun 25 '21 at 23:01
  • If you're using windows, the Calculator app in Programmer mode can check for you. Be sure to toggle the button that shows QWORD or DWORD or BYTE to BYTE – Mark Wheeler Jun 25 '21 at 23:22
  • I am using a mac – Big Mike from Laos Jun 25 '21 at 23:35
  • 1
    114 base 10 is not 01101000 base 2, 2's complement or otherwise. – Steve Summit Jun 25 '21 at 23:39
  • @SteveSummit would you be able to show me what to do? I still don't get it – Big Mike from Laos Jun 25 '21 at 23:45
  • You can use a command-line calculator like `calc` http://www.isthe.com/chongo/tech/comp/calc/index.html if you understand 2's complement. You can tell it to output numbers in an alternate format, like `base2(16)` to set the 2nd output format to hex, or base2(2) for binary. `45+114` is 0x9f, so it doesn't carry-out from the low 8 bits, but it does have signed overflow. – Peter Cordes Jun 25 '21 at 23:46

1 Answers1

1

This looks like homework, so I'm not going to give you the answer, but I can help you find your answer. Please fill in all the blanks, and answer the interspersed questions.

45 in binary is ________[1]
114 in binary is ________[2]

These can both be represented in 8 bits. But when they're represented in 8 bits, if interpreted as 2's complement, would they be interpreted as negative or positive numbers? (My guess is that they should be positive, since they're both less than 127, which is the largest number that can be represented as 8 bit 2's complement.)

Now do binary addition of [1] + [2], giving the result ________[3]

Now, when [3] is represented in 8 bits, if interpreted as 2's complement, would it be interpreted as a negative or positive number? (If I do the math in decimal, I notice that 45 + 107 is 159, which is greater than 127, so I'm guessing there's going to be an "issue" here.)

Finally, what is the actual value of [3], interpreted as 2's complement? Fill it in here: ___[4]

Is [4] equal to 159? If not, why not?

What do you get if you take [4], remove the minus sign to convert it to a positive number (that is, take the absolute value), and add it (as an ordinary number) to 159? Does that sum look familiar?


Now, to the other problem.

Figure out the 8-bit 2's complement representation of -93: ________[5]
Figure out the 8-bit 2's complement representation of -35: ________[6]

As a side problem, just for fun, interpret [5] as an ordinary (not 2's complement) 8-bit binary number. (It will therefore be a positive number.) Answer: ___[7]

Do the same with [6]. Answer: ___[8]

Compute (using ordinary arithmetic) [7] + 93. Does that number look familiar?

Compute (using ordinary arithmetic) [8] + 35. Does that number look familiar?

Now add together [7] and [8] in binary: _________[9]. How many bits does the result have? Is it more than 8?

If it's more than 8 bits, throw away the top bit, to leave 8 bits: ________[10]

Interpreting [10] as an ordinary binary number, what is it? ___[11]

Interpreting [10] as a 2's complement number, what is it? ___[12] (This is a little tricky, be careful.)

Finally, if you do the problem using ordinary arithmetic, does -93 plus -35 equal [12]?


If you're still feeling lost, here are a few different examples to think about. Let's look at the number 37.

37 in binary is 00100101
the two's complement of 00100101 is 11011011
so -37 in two's complement is 11011011

Also, as a sort of a double-check, notice that if we take 11011011 as an ordinary binary number it's 219. And then notice that 256 - 37 = 219.

Now, let's do a simple addition problem. I'm going to show it in three columns: (A), (B), and (C). Column (B) is binary. Column (A) is the binary numbers in column (B), interpreted as 2's complement. Column (C) is the binary numbers in column (B), interpreted as ordinary numbers (not 2's complement).

Also, the 8-bit sum overflows, so there are two different "bottom lines": the full 9-bit sum, and the sum after throwing away the 9th or carry bit.

   (A)         (B)        (C)
    50       00110010      50
+  -37    +  11011011   + 219
   ---      ---------     ---
  (n/a)     100001101     269
    13       00001101      13

One more thing: notice that 269 - 13 = 256.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • 45 in binary is: 101101, 114 in binary is: 1110010. The addition gives them 11011111, which as 2's compliment is 00100001. Which is 33 in decimal. Not sure where to go from here @SteveSummit – Big Mike from Laos Jun 26 '21 at 01:06
  • 2
    @JohnGoldstein Trouble is, 101101 + 1110010 is *not* 11011111. So pease try again. – Steve Summit Jun 26 '21 at 13:23