Search code examples
binaryoverflowadditiontwos-complementunderflow

Two's Complement binary addition


I have a program that adds 2 two's complement binary numbers and spits out the output. They are read from a file as the first binary number being in two's complement. The second binary number being in biased notation. To change form biased to two's complement we flip the left most bit. We then add the two binary numbers and give the output to the file. What I am having trouble with is understanding the output. Here is the output where the first binary number is in two's complement and the second in biased notation.

   01000000 <- 2's complement         64
+  11000000 <- biased notation        64
-----------------------------------------------
   10000000 <- 2's complement   Overflow

   01111111 <- 2's complement        127
+  00000000 <- biased notation      -128
-----------------------------------------------
   11111111 <- 2's complement         -1

   00000011 <- 2's complement          3
+  10000111 <- biased notation         7
-----------------------------------------------
   00000110 <- 2's complement          6

   00001111 <- 2's complement         15
+  10000111 <- biased notation         7
-----------------------------------------------
   00010010 <- 2's complement         18

   10000000 <- 2's complement       -128
+  11111111 <- biased notation       127
-----------------------------------------------
   11111111 <- 2's complement         -1

   11110000 <- 2's complement        -16
+  10001000 <- biased notation         8
-----------------------------------------------
   11111000 <- 2's complement         -8

   10000001 <- 2's complement       -127
+  00000001 <- biased notation      -127
-----------------------------------------------
   00000010 <- 2's complement   Underflow

   01111111 <- 2's complement        127
+  00000000 <- biased notation      -128
-----------------------------------------------
   11111111 <- 2's complement         -1

   01110101 <- 2's complement        117
+  11010001 <- biased notation        81
-----------------------------------------------
   01000110 <- 2's complement         70

   00000000 <- 2's complement          0
+  10000000 <- biased notation         0
-----------------------------------------------
   00000000 <- 2's complement          0

   00001111 <- 2's complement         15
+  11110000 <- biased notation       112
-----------------------------------------------
   01111111 <- 2's complement        127

   00001111 <- 2's complement         15
+  10000001 <- biased notation         1
-----------------------------------------------
   00010000 <- 2's complement         16

   00000111 <- 2's complement          7
+  11110000 <- biased notation       112
-----------------------------------------------
   01110111 <- 2's complement        119

   11111111 <- 2's complement         -1
+  01111111 <- biased notation        -1
-----------------------------------------------
   10101010 <- 2's complement        -86

   00000000 <- 2's complement          0
+  10000000 <- biased notation         0
-----------------------------------------------
   00000000 <- 2's complement          0

   11111111 <- 2's complement         -1
+  11111111 <- biased notation       127
-----------------------------------------------
   00101010 <- 2's complement         42

Given the third example says 3 + 7 = 6. Is this even correct? It doesn't seem right, but other examples are correct. Like -16 + 8 = -8.

So my question is... Is the output of this file correct?


Solution

  • It seems like whenever you have 3 carries in a row, it drops a 1. Same with the next one, 15 + 7. It isn't doing the carry correctly.

      11  <- carry
     0011 = 3
    +0111 = 7
     1010 = 10