Signed Number's Binary Addition

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I'm going to discuss about Signed Number's Binary addition, I searched about it and even read books. Now I make little changes in it's logic and start my own logic to solve it.



Let me show 4 bit example by Book Method.



-5+3


Book told me that I should take two's complement of 5 to add in 3's binary. 1011 is two's complement of 5. now add with 3's binary. 0011.



1011 <----- -5 ( 5's two's complement )
0011 <----- 3
---
1110 <----- -2 ( leftsigned bit is 1 therefore it is in two's complement form )
0010 <----- 2 ( Again two's complement of answer to show original answer )


But the answer sign bit is positive that's mean answer is 2?



Now I tried again and again and find a stupid method to solve the above or any signed number's binary addition. Let me explain with my stupid logic.



Note: I will never change sign bit during once complement.


First I will take the complement of 5 with no effect on sign bit during once complement.



1101 <----- -5 ( Sign bit showing it's -5 )
1010 <----- -5's once complement ( never change sign bit )
1
----
1011 <----- -5 two's complement.


Now start addition with 3's binary.



1011 <----- -5 (two's complement)
0011 <----- 3 ( with positive sign bit )
----
1110 <-- Signbit 1 mean it's in two's complement form. Again two's complement to get answer
1001 <----- Once complement (never change sign bit during Once Complement)
1
----
1010 <----- -2 ( Sign bit is negative(1). it's showing that it is -2 )


Now I just want to clear that could my method satisfy the Signed number's binary addition? or it is a stupid method.










share|cite|improve this question





















  • It looks like you are converting a number in 2's complement form into signed magnitude form in the end. Not bad. :)
    – Bhaskar
    Jun 19 '15 at 16:06










  • But you are still using 2's complements of numbers for addition, only the end result's form is changed.
    – Bhaskar
    Jun 19 '15 at 16:08










  • @L16H7 I'm writing any binary with signed magnitude and then start the 2's complement.. But I'm not changing sign bit during complement..!! It's generating real answer and the end..!! what you say? Is it right way?
    – Let Do it
    Jun 20 '15 at 7:06










  • What you are doing is you convert numbers from signed magnitude form into 2's complement then add them and convert back into signed magnitude. What you discovered here is method of conversion between signed magnitude and 2's complement and you apply it at the beginning and ending of addtion process. Do you get what I'm trying say?
    – Bhaskar
    Jun 20 '15 at 7:32














up vote
0
down vote

favorite












I'm going to discuss about Signed Number's Binary addition, I searched about it and even read books. Now I make little changes in it's logic and start my own logic to solve it.



Let me show 4 bit example by Book Method.



-5+3


Book told me that I should take two's complement of 5 to add in 3's binary. 1011 is two's complement of 5. now add with 3's binary. 0011.



1011 <----- -5 ( 5's two's complement )
0011 <----- 3
---
1110 <----- -2 ( leftsigned bit is 1 therefore it is in two's complement form )
0010 <----- 2 ( Again two's complement of answer to show original answer )


But the answer sign bit is positive that's mean answer is 2?



Now I tried again and again and find a stupid method to solve the above or any signed number's binary addition. Let me explain with my stupid logic.



Note: I will never change sign bit during once complement.


First I will take the complement of 5 with no effect on sign bit during once complement.



1101 <----- -5 ( Sign bit showing it's -5 )
1010 <----- -5's once complement ( never change sign bit )
1
----
1011 <----- -5 two's complement.


Now start addition with 3's binary.



1011 <----- -5 (two's complement)
0011 <----- 3 ( with positive sign bit )
----
1110 <-- Signbit 1 mean it's in two's complement form. Again two's complement to get answer
1001 <----- Once complement (never change sign bit during Once Complement)
1
----
1010 <----- -2 ( Sign bit is negative(1). it's showing that it is -2 )


Now I just want to clear that could my method satisfy the Signed number's binary addition? or it is a stupid method.










share|cite|improve this question





















  • It looks like you are converting a number in 2's complement form into signed magnitude form in the end. Not bad. :)
    – Bhaskar
    Jun 19 '15 at 16:06










  • But you are still using 2's complements of numbers for addition, only the end result's form is changed.
    – Bhaskar
    Jun 19 '15 at 16:08










  • @L16H7 I'm writing any binary with signed magnitude and then start the 2's complement.. But I'm not changing sign bit during complement..!! It's generating real answer and the end..!! what you say? Is it right way?
    – Let Do it
    Jun 20 '15 at 7:06










  • What you are doing is you convert numbers from signed magnitude form into 2's complement then add them and convert back into signed magnitude. What you discovered here is method of conversion between signed magnitude and 2's complement and you apply it at the beginning and ending of addtion process. Do you get what I'm trying say?
    – Bhaskar
    Jun 20 '15 at 7:32












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm going to discuss about Signed Number's Binary addition, I searched about it and even read books. Now I make little changes in it's logic and start my own logic to solve it.



Let me show 4 bit example by Book Method.



-5+3


Book told me that I should take two's complement of 5 to add in 3's binary. 1011 is two's complement of 5. now add with 3's binary. 0011.



1011 <----- -5 ( 5's two's complement )
0011 <----- 3
---
1110 <----- -2 ( leftsigned bit is 1 therefore it is in two's complement form )
0010 <----- 2 ( Again two's complement of answer to show original answer )


But the answer sign bit is positive that's mean answer is 2?



Now I tried again and again and find a stupid method to solve the above or any signed number's binary addition. Let me explain with my stupid logic.



Note: I will never change sign bit during once complement.


First I will take the complement of 5 with no effect on sign bit during once complement.



1101 <----- -5 ( Sign bit showing it's -5 )
1010 <----- -5's once complement ( never change sign bit )
1
----
1011 <----- -5 two's complement.


Now start addition with 3's binary.



1011 <----- -5 (two's complement)
0011 <----- 3 ( with positive sign bit )
----
1110 <-- Signbit 1 mean it's in two's complement form. Again two's complement to get answer
1001 <----- Once complement (never change sign bit during Once Complement)
1
----
1010 <----- -2 ( Sign bit is negative(1). it's showing that it is -2 )


Now I just want to clear that could my method satisfy the Signed number's binary addition? or it is a stupid method.










share|cite|improve this question













I'm going to discuss about Signed Number's Binary addition, I searched about it and even read books. Now I make little changes in it's logic and start my own logic to solve it.



Let me show 4 bit example by Book Method.



-5+3


Book told me that I should take two's complement of 5 to add in 3's binary. 1011 is two's complement of 5. now add with 3's binary. 0011.



1011 <----- -5 ( 5's two's complement )
0011 <----- 3
---
1110 <----- -2 ( leftsigned bit is 1 therefore it is in two's complement form )
0010 <----- 2 ( Again two's complement of answer to show original answer )


But the answer sign bit is positive that's mean answer is 2?



Now I tried again and again and find a stupid method to solve the above or any signed number's binary addition. Let me explain with my stupid logic.



Note: I will never change sign bit during once complement.


First I will take the complement of 5 with no effect on sign bit during once complement.



1101 <----- -5 ( Sign bit showing it's -5 )
1010 <----- -5's once complement ( never change sign bit )
1
----
1011 <----- -5 two's complement.


Now start addition with 3's binary.



1011 <----- -5 (two's complement)
0011 <----- 3 ( with positive sign bit )
----
1110 <-- Signbit 1 mean it's in two's complement form. Again two's complement to get answer
1001 <----- Once complement (never change sign bit during Once Complement)
1
----
1010 <----- -2 ( Sign bit is negative(1). it's showing that it is -2 )


Now I just want to clear that could my method satisfy the Signed number's binary addition? or it is a stupid method.







binary






share|cite|improve this question













share|cite|improve this question











share|cite|improve this question




share|cite|improve this question










asked Jun 19 '15 at 15:25









Let Do it

10112




10112











  • It looks like you are converting a number in 2's complement form into signed magnitude form in the end. Not bad. :)
    – Bhaskar
    Jun 19 '15 at 16:06










  • But you are still using 2's complements of numbers for addition, only the end result's form is changed.
    – Bhaskar
    Jun 19 '15 at 16:08










  • @L16H7 I'm writing any binary with signed magnitude and then start the 2's complement.. But I'm not changing sign bit during complement..!! It's generating real answer and the end..!! what you say? Is it right way?
    – Let Do it
    Jun 20 '15 at 7:06










  • What you are doing is you convert numbers from signed magnitude form into 2's complement then add them and convert back into signed magnitude. What you discovered here is method of conversion between signed magnitude and 2's complement and you apply it at the beginning and ending of addtion process. Do you get what I'm trying say?
    – Bhaskar
    Jun 20 '15 at 7:32
















  • It looks like you are converting a number in 2's complement form into signed magnitude form in the end. Not bad. :)
    – Bhaskar
    Jun 19 '15 at 16:06










  • But you are still using 2's complements of numbers for addition, only the end result's form is changed.
    – Bhaskar
    Jun 19 '15 at 16:08










  • @L16H7 I'm writing any binary with signed magnitude and then start the 2's complement.. But I'm not changing sign bit during complement..!! It's generating real answer and the end..!! what you say? Is it right way?
    – Let Do it
    Jun 20 '15 at 7:06










  • What you are doing is you convert numbers from signed magnitude form into 2's complement then add them and convert back into signed magnitude. What you discovered here is method of conversion between signed magnitude and 2's complement and you apply it at the beginning and ending of addtion process. Do you get what I'm trying say?
    – Bhaskar
    Jun 20 '15 at 7:32















It looks like you are converting a number in 2's complement form into signed magnitude form in the end. Not bad. :)
– Bhaskar
Jun 19 '15 at 16:06




It looks like you are converting a number in 2's complement form into signed magnitude form in the end. Not bad. :)
– Bhaskar
Jun 19 '15 at 16:06












But you are still using 2's complements of numbers for addition, only the end result's form is changed.
– Bhaskar
Jun 19 '15 at 16:08




But you are still using 2's complements of numbers for addition, only the end result's form is changed.
– Bhaskar
Jun 19 '15 at 16:08












@L16H7 I'm writing any binary with signed magnitude and then start the 2's complement.. But I'm not changing sign bit during complement..!! It's generating real answer and the end..!! what you say? Is it right way?
– Let Do it
Jun 20 '15 at 7:06




@L16H7 I'm writing any binary with signed magnitude and then start the 2's complement.. But I'm not changing sign bit during complement..!! It's generating real answer and the end..!! what you say? Is it right way?
– Let Do it
Jun 20 '15 at 7:06












What you are doing is you convert numbers from signed magnitude form into 2's complement then add them and convert back into signed magnitude. What you discovered here is method of conversion between signed magnitude and 2's complement and you apply it at the beginning and ending of addtion process. Do you get what I'm trying say?
– Bhaskar
Jun 20 '15 at 7:32




What you are doing is you convert numbers from signed magnitude form into 2's complement then add them and convert back into signed magnitude. What you discovered here is method of conversion between signed magnitude and 2's complement and you apply it at the beginning and ending of addtion process. Do you get what I'm trying say?
– Bhaskar
Jun 20 '15 at 7:32










2 Answers
2






active

oldest

votes

















up vote
0
down vote













Here's a good page that explains adding signed and unsigned binary numbers, and using the 4-bit 2's complement.



Hope that helps.



EDIT: Just noticed this was asked 4 months ago; I hope he managed to find an answer. :-)






share|cite|improve this answer





























    up vote
    0
    down vote













    Let's take a second look at the "book" method of adding $-5$ and $3$ in four-bit two's-complement arithmetic.



    In four-bit two's-complement binary, $-5$ is represented by $1011$ and $3$ is represented by $0011.$ The sum is $1011 + 0011 = 1110,$ which is the four-bit two's-complement representation of $-2.$ And that's the answer: $-2.$



    The only reason to do any further operations on the result is if you want to convert the result back to ordinary decimal notation and don't instantly recognize that $1110$ represents $-2.$
    One way to do that is to take the two's complement of $1110,$ which is $0010.$ Taking the two's complement of a number changes the sign of the number in the two's-complement representation.
    If reversing the sign of $1110$ gives us $0010,$ which represents $2,$ then what does $1110$ represent? It can only represent $-2.$
    Again, that 's the answer: $-2.$




    The rest of the question presents an interesting procedure for adding binary representations of integers. Instead of using two's-complement exclusively, however, this method begins with the two operands ($-5$ and $3$) in a four-bit signed-magnitude representation, and ends with the result ($-2$) in four-bit signed-magnitude representation.



    The method has three major steps:



    1. Convert the signed-magnitude representation to two's complement.

    2. Add the two's-complement representations.

    3. Convert the result from two's complement to signed magnitude.

    The two's-complement addition is performed in the conventional way, so we merely have to check that the conversions from signed-magnitude to two's-complement and back again are correct.



    Positive numbers are "converted" by leaving their bits completely unchanged.
    Only negative numbers have their bits changed by the conversion.
    In order to be able to reason about the conversion mathematically, I'll evaluate each four-bit binary number as an unsigned integer, although it can still also be interpreted as signed magnitude or two's complement.



    If a four-bit signed-magnitude binary number represents $x,$ where $x < 0,$
    then the bits of that representation have unsigned value
    $8 - lvert xrvert.$ (The sign bit has place value $8,$ and the rightmost three bits have value $lvert xrvert.$)
    The first step is to take the one's complement of the three magnitude bits,
    which is equivalent to subtracting them from the three-bit number $111,$
    that is, from $7.$ The sign bit remains unchanged, so after this step
    we have a four-bit number with unsigned value
    $8 + (7 - lvert xrvert) = 15 - lvert xrvert.$
    The second step is to add $1.$ The resulting number has unsigned value
    $(15 - lvert xrvert) + 1 = 16 - lvert xrvert = 16 + x,$
    since $x < 0.$
    And $16 + x$ is the unsigned value of the four-bit two's-complement representation of a number $x$ when $x < 0.$
    Hence the conversion is always correct.



    More generally, for an $n$-bit signed-magnitude representation of a
    negative number $x,$ the
    unsigned value of the signed-magnitude representation is
    $2^n-1 + lvert xrvert,$ after taking the one's complement of the magnitude we have
    $2^n-1 + (2^n-1 - 1 - lvert xrvert) = 2^n - 1 - lvert xrvert,$
    and after adding $1$ we have
    $(2^n - 1 - lvert xrvert) + 1 = 2^n - lvert xrvert = 2^n + x,$
    the two's complement representation of $x.$ Since the signed-magnitude
    representation can only represent a number $x$ if
    $lvert xrvert leq 2^n-1 - 1,$ there is no possibility of overflow,
    and this conversion will always work.



    Note that if the signed-magnitude number is $-0$ (sign bit $1$ with magnitude $0$), the procedure above produces a number with all bits set to $1$ after the one's complement, and adding $1$ to this results in all bits set to $0.$
    That is, this signed-magnitude representation is correctly converted to $0$ in two's complement.



    To convert from two's complement to signed magnitude, again positive numbers are not changed, and only negative numbers require manipulation of their bits.
    Clearly the procedure cannot work when $x = -2^n-1,$ which is the most negative possible number that can be represented in $n$-bit two's complement,
    because the least number that can be represented in $n$-bit signed magnitude is $-(2^n-1 - 1).$
    So let's assume that $-(2^n-1 - 1) leq x leq -1,$ that is, $x$ can be
    any of the possible negative integers in $n$-bit two's-complement binary representation except $-2^n-1$
    Then the $n$-bit two's-complement representation of $x$ is a binary number
    with unsigned value
    $2^n + x = 2^n - lvert x rvert geq 2^n-1 + (2^n-1 - lvert x rvert).$
    That is, the rightmost $n-1$ bits have unsigned value
    $2^n-1 - lvert x rvert.$
    We take the one's complement of these bits,
    $(2^n-1 - 1) - (2^n-1 - lvert x rvert) = lvert x rvert - 1,$
    and put these to the right of the sign bit, so we have a binary number
    with unsigned value $2^n-1 + lvert x rvert - 1.$
    The next step is to add $1$ to the this, with the result
    $2^n-1 + lvert x rvert,$ which is the $n$-bit signed-magnitude representation of $x$ when $x < 0.$



    In summary, the proposed method does indeed give correct results in all cases except when overflow occurs, and those are precisely the cases where it is not possible to represent the correct result in signed magnitude
    (that is, those are the cases in which no algorithm can give the correct answer).






    share|cite|improve this answer




















      Your Answer




      StackExchange.ifUsing("editor", function ()
      return StackExchange.using("mathjaxEditing", function ()
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
      );
      );
      , "mathjax-editing");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "69"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      noCode: true, onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f1331621%2fsigned-numbers-binary-addition%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      Here's a good page that explains adding signed and unsigned binary numbers, and using the 4-bit 2's complement.



      Hope that helps.



      EDIT: Just noticed this was asked 4 months ago; I hope he managed to find an answer. :-)






      share|cite|improve this answer


























        up vote
        0
        down vote













        Here's a good page that explains adding signed and unsigned binary numbers, and using the 4-bit 2's complement.



        Hope that helps.



        EDIT: Just noticed this was asked 4 months ago; I hope he managed to find an answer. :-)






        share|cite|improve this answer
























          up vote
          0
          down vote










          up vote
          0
          down vote









          Here's a good page that explains adding signed and unsigned binary numbers, and using the 4-bit 2's complement.



          Hope that helps.



          EDIT: Just noticed this was asked 4 months ago; I hope he managed to find an answer. :-)






          share|cite|improve this answer














          Here's a good page that explains adding signed and unsigned binary numbers, and using the 4-bit 2's complement.



          Hope that helps.



          EDIT: Just noticed this was asked 4 months ago; I hope he managed to find an answer. :-)







          share|cite|improve this answer














          share|cite|improve this answer



          share|cite|improve this answer








          edited Oct 27 '15 at 22:40

























          answered Oct 27 '15 at 22:33









          Ole Drews Jensen

          14211




          14211




















              up vote
              0
              down vote













              Let's take a second look at the "book" method of adding $-5$ and $3$ in four-bit two's-complement arithmetic.



              In four-bit two's-complement binary, $-5$ is represented by $1011$ and $3$ is represented by $0011.$ The sum is $1011 + 0011 = 1110,$ which is the four-bit two's-complement representation of $-2.$ And that's the answer: $-2.$



              The only reason to do any further operations on the result is if you want to convert the result back to ordinary decimal notation and don't instantly recognize that $1110$ represents $-2.$
              One way to do that is to take the two's complement of $1110,$ which is $0010.$ Taking the two's complement of a number changes the sign of the number in the two's-complement representation.
              If reversing the sign of $1110$ gives us $0010,$ which represents $2,$ then what does $1110$ represent? It can only represent $-2.$
              Again, that 's the answer: $-2.$




              The rest of the question presents an interesting procedure for adding binary representations of integers. Instead of using two's-complement exclusively, however, this method begins with the two operands ($-5$ and $3$) in a four-bit signed-magnitude representation, and ends with the result ($-2$) in four-bit signed-magnitude representation.



              The method has three major steps:



              1. Convert the signed-magnitude representation to two's complement.

              2. Add the two's-complement representations.

              3. Convert the result from two's complement to signed magnitude.

              The two's-complement addition is performed in the conventional way, so we merely have to check that the conversions from signed-magnitude to two's-complement and back again are correct.



              Positive numbers are "converted" by leaving their bits completely unchanged.
              Only negative numbers have their bits changed by the conversion.
              In order to be able to reason about the conversion mathematically, I'll evaluate each four-bit binary number as an unsigned integer, although it can still also be interpreted as signed magnitude or two's complement.



              If a four-bit signed-magnitude binary number represents $x,$ where $x < 0,$
              then the bits of that representation have unsigned value
              $8 - lvert xrvert.$ (The sign bit has place value $8,$ and the rightmost three bits have value $lvert xrvert.$)
              The first step is to take the one's complement of the three magnitude bits,
              which is equivalent to subtracting them from the three-bit number $111,$
              that is, from $7.$ The sign bit remains unchanged, so after this step
              we have a four-bit number with unsigned value
              $8 + (7 - lvert xrvert) = 15 - lvert xrvert.$
              The second step is to add $1.$ The resulting number has unsigned value
              $(15 - lvert xrvert) + 1 = 16 - lvert xrvert = 16 + x,$
              since $x < 0.$
              And $16 + x$ is the unsigned value of the four-bit two's-complement representation of a number $x$ when $x < 0.$
              Hence the conversion is always correct.



              More generally, for an $n$-bit signed-magnitude representation of a
              negative number $x,$ the
              unsigned value of the signed-magnitude representation is
              $2^n-1 + lvert xrvert,$ after taking the one's complement of the magnitude we have
              $2^n-1 + (2^n-1 - 1 - lvert xrvert) = 2^n - 1 - lvert xrvert,$
              and after adding $1$ we have
              $(2^n - 1 - lvert xrvert) + 1 = 2^n - lvert xrvert = 2^n + x,$
              the two's complement representation of $x.$ Since the signed-magnitude
              representation can only represent a number $x$ if
              $lvert xrvert leq 2^n-1 - 1,$ there is no possibility of overflow,
              and this conversion will always work.



              Note that if the signed-magnitude number is $-0$ (sign bit $1$ with magnitude $0$), the procedure above produces a number with all bits set to $1$ after the one's complement, and adding $1$ to this results in all bits set to $0.$
              That is, this signed-magnitude representation is correctly converted to $0$ in two's complement.



              To convert from two's complement to signed magnitude, again positive numbers are not changed, and only negative numbers require manipulation of their bits.
              Clearly the procedure cannot work when $x = -2^n-1,$ which is the most negative possible number that can be represented in $n$-bit two's complement,
              because the least number that can be represented in $n$-bit signed magnitude is $-(2^n-1 - 1).$
              So let's assume that $-(2^n-1 - 1) leq x leq -1,$ that is, $x$ can be
              any of the possible negative integers in $n$-bit two's-complement binary representation except $-2^n-1$
              Then the $n$-bit two's-complement representation of $x$ is a binary number
              with unsigned value
              $2^n + x = 2^n - lvert x rvert geq 2^n-1 + (2^n-1 - lvert x rvert).$
              That is, the rightmost $n-1$ bits have unsigned value
              $2^n-1 - lvert x rvert.$
              We take the one's complement of these bits,
              $(2^n-1 - 1) - (2^n-1 - lvert x rvert) = lvert x rvert - 1,$
              and put these to the right of the sign bit, so we have a binary number
              with unsigned value $2^n-1 + lvert x rvert - 1.$
              The next step is to add $1$ to the this, with the result
              $2^n-1 + lvert x rvert,$ which is the $n$-bit signed-magnitude representation of $x$ when $x < 0.$



              In summary, the proposed method does indeed give correct results in all cases except when overflow occurs, and those are precisely the cases where it is not possible to represent the correct result in signed magnitude
              (that is, those are the cases in which no algorithm can give the correct answer).






              share|cite|improve this answer
























                up vote
                0
                down vote













                Let's take a second look at the "book" method of adding $-5$ and $3$ in four-bit two's-complement arithmetic.



                In four-bit two's-complement binary, $-5$ is represented by $1011$ and $3$ is represented by $0011.$ The sum is $1011 + 0011 = 1110,$ which is the four-bit two's-complement representation of $-2.$ And that's the answer: $-2.$



                The only reason to do any further operations on the result is if you want to convert the result back to ordinary decimal notation and don't instantly recognize that $1110$ represents $-2.$
                One way to do that is to take the two's complement of $1110,$ which is $0010.$ Taking the two's complement of a number changes the sign of the number in the two's-complement representation.
                If reversing the sign of $1110$ gives us $0010,$ which represents $2,$ then what does $1110$ represent? It can only represent $-2.$
                Again, that 's the answer: $-2.$




                The rest of the question presents an interesting procedure for adding binary representations of integers. Instead of using two's-complement exclusively, however, this method begins with the two operands ($-5$ and $3$) in a four-bit signed-magnitude representation, and ends with the result ($-2$) in four-bit signed-magnitude representation.



                The method has three major steps:



                1. Convert the signed-magnitude representation to two's complement.

                2. Add the two's-complement representations.

                3. Convert the result from two's complement to signed magnitude.

                The two's-complement addition is performed in the conventional way, so we merely have to check that the conversions from signed-magnitude to two's-complement and back again are correct.



                Positive numbers are "converted" by leaving their bits completely unchanged.
                Only negative numbers have their bits changed by the conversion.
                In order to be able to reason about the conversion mathematically, I'll evaluate each four-bit binary number as an unsigned integer, although it can still also be interpreted as signed magnitude or two's complement.



                If a four-bit signed-magnitude binary number represents $x,$ where $x < 0,$
                then the bits of that representation have unsigned value
                $8 - lvert xrvert.$ (The sign bit has place value $8,$ and the rightmost three bits have value $lvert xrvert.$)
                The first step is to take the one's complement of the three magnitude bits,
                which is equivalent to subtracting them from the three-bit number $111,$
                that is, from $7.$ The sign bit remains unchanged, so after this step
                we have a four-bit number with unsigned value
                $8 + (7 - lvert xrvert) = 15 - lvert xrvert.$
                The second step is to add $1.$ The resulting number has unsigned value
                $(15 - lvert xrvert) + 1 = 16 - lvert xrvert = 16 + x,$
                since $x < 0.$
                And $16 + x$ is the unsigned value of the four-bit two's-complement representation of a number $x$ when $x < 0.$
                Hence the conversion is always correct.



                More generally, for an $n$-bit signed-magnitude representation of a
                negative number $x,$ the
                unsigned value of the signed-magnitude representation is
                $2^n-1 + lvert xrvert,$ after taking the one's complement of the magnitude we have
                $2^n-1 + (2^n-1 - 1 - lvert xrvert) = 2^n - 1 - lvert xrvert,$
                and after adding $1$ we have
                $(2^n - 1 - lvert xrvert) + 1 = 2^n - lvert xrvert = 2^n + x,$
                the two's complement representation of $x.$ Since the signed-magnitude
                representation can only represent a number $x$ if
                $lvert xrvert leq 2^n-1 - 1,$ there is no possibility of overflow,
                and this conversion will always work.



                Note that if the signed-magnitude number is $-0$ (sign bit $1$ with magnitude $0$), the procedure above produces a number with all bits set to $1$ after the one's complement, and adding $1$ to this results in all bits set to $0.$
                That is, this signed-magnitude representation is correctly converted to $0$ in two's complement.



                To convert from two's complement to signed magnitude, again positive numbers are not changed, and only negative numbers require manipulation of their bits.
                Clearly the procedure cannot work when $x = -2^n-1,$ which is the most negative possible number that can be represented in $n$-bit two's complement,
                because the least number that can be represented in $n$-bit signed magnitude is $-(2^n-1 - 1).$
                So let's assume that $-(2^n-1 - 1) leq x leq -1,$ that is, $x$ can be
                any of the possible negative integers in $n$-bit two's-complement binary representation except $-2^n-1$
                Then the $n$-bit two's-complement representation of $x$ is a binary number
                with unsigned value
                $2^n + x = 2^n - lvert x rvert geq 2^n-1 + (2^n-1 - lvert x rvert).$
                That is, the rightmost $n-1$ bits have unsigned value
                $2^n-1 - lvert x rvert.$
                We take the one's complement of these bits,
                $(2^n-1 - 1) - (2^n-1 - lvert x rvert) = lvert x rvert - 1,$
                and put these to the right of the sign bit, so we have a binary number
                with unsigned value $2^n-1 + lvert x rvert - 1.$
                The next step is to add $1$ to the this, with the result
                $2^n-1 + lvert x rvert,$ which is the $n$-bit signed-magnitude representation of $x$ when $x < 0.$



                In summary, the proposed method does indeed give correct results in all cases except when overflow occurs, and those are precisely the cases where it is not possible to represent the correct result in signed magnitude
                (that is, those are the cases in which no algorithm can give the correct answer).






                share|cite|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Let's take a second look at the "book" method of adding $-5$ and $3$ in four-bit two's-complement arithmetic.



                  In four-bit two's-complement binary, $-5$ is represented by $1011$ and $3$ is represented by $0011.$ The sum is $1011 + 0011 = 1110,$ which is the four-bit two's-complement representation of $-2.$ And that's the answer: $-2.$



                  The only reason to do any further operations on the result is if you want to convert the result back to ordinary decimal notation and don't instantly recognize that $1110$ represents $-2.$
                  One way to do that is to take the two's complement of $1110,$ which is $0010.$ Taking the two's complement of a number changes the sign of the number in the two's-complement representation.
                  If reversing the sign of $1110$ gives us $0010,$ which represents $2,$ then what does $1110$ represent? It can only represent $-2.$
                  Again, that 's the answer: $-2.$




                  The rest of the question presents an interesting procedure for adding binary representations of integers. Instead of using two's-complement exclusively, however, this method begins with the two operands ($-5$ and $3$) in a four-bit signed-magnitude representation, and ends with the result ($-2$) in four-bit signed-magnitude representation.



                  The method has three major steps:



                  1. Convert the signed-magnitude representation to two's complement.

                  2. Add the two's-complement representations.

                  3. Convert the result from two's complement to signed magnitude.

                  The two's-complement addition is performed in the conventional way, so we merely have to check that the conversions from signed-magnitude to two's-complement and back again are correct.



                  Positive numbers are "converted" by leaving their bits completely unchanged.
                  Only negative numbers have their bits changed by the conversion.
                  In order to be able to reason about the conversion mathematically, I'll evaluate each four-bit binary number as an unsigned integer, although it can still also be interpreted as signed magnitude or two's complement.



                  If a four-bit signed-magnitude binary number represents $x,$ where $x < 0,$
                  then the bits of that representation have unsigned value
                  $8 - lvert xrvert.$ (The sign bit has place value $8,$ and the rightmost three bits have value $lvert xrvert.$)
                  The first step is to take the one's complement of the three magnitude bits,
                  which is equivalent to subtracting them from the three-bit number $111,$
                  that is, from $7.$ The sign bit remains unchanged, so after this step
                  we have a four-bit number with unsigned value
                  $8 + (7 - lvert xrvert) = 15 - lvert xrvert.$
                  The second step is to add $1.$ The resulting number has unsigned value
                  $(15 - lvert xrvert) + 1 = 16 - lvert xrvert = 16 + x,$
                  since $x < 0.$
                  And $16 + x$ is the unsigned value of the four-bit two's-complement representation of a number $x$ when $x < 0.$
                  Hence the conversion is always correct.



                  More generally, for an $n$-bit signed-magnitude representation of a
                  negative number $x,$ the
                  unsigned value of the signed-magnitude representation is
                  $2^n-1 + lvert xrvert,$ after taking the one's complement of the magnitude we have
                  $2^n-1 + (2^n-1 - 1 - lvert xrvert) = 2^n - 1 - lvert xrvert,$
                  and after adding $1$ we have
                  $(2^n - 1 - lvert xrvert) + 1 = 2^n - lvert xrvert = 2^n + x,$
                  the two's complement representation of $x.$ Since the signed-magnitude
                  representation can only represent a number $x$ if
                  $lvert xrvert leq 2^n-1 - 1,$ there is no possibility of overflow,
                  and this conversion will always work.



                  Note that if the signed-magnitude number is $-0$ (sign bit $1$ with magnitude $0$), the procedure above produces a number with all bits set to $1$ after the one's complement, and adding $1$ to this results in all bits set to $0.$
                  That is, this signed-magnitude representation is correctly converted to $0$ in two's complement.



                  To convert from two's complement to signed magnitude, again positive numbers are not changed, and only negative numbers require manipulation of their bits.
                  Clearly the procedure cannot work when $x = -2^n-1,$ which is the most negative possible number that can be represented in $n$-bit two's complement,
                  because the least number that can be represented in $n$-bit signed magnitude is $-(2^n-1 - 1).$
                  So let's assume that $-(2^n-1 - 1) leq x leq -1,$ that is, $x$ can be
                  any of the possible negative integers in $n$-bit two's-complement binary representation except $-2^n-1$
                  Then the $n$-bit two's-complement representation of $x$ is a binary number
                  with unsigned value
                  $2^n + x = 2^n - lvert x rvert geq 2^n-1 + (2^n-1 - lvert x rvert).$
                  That is, the rightmost $n-1$ bits have unsigned value
                  $2^n-1 - lvert x rvert.$
                  We take the one's complement of these bits,
                  $(2^n-1 - 1) - (2^n-1 - lvert x rvert) = lvert x rvert - 1,$
                  and put these to the right of the sign bit, so we have a binary number
                  with unsigned value $2^n-1 + lvert x rvert - 1.$
                  The next step is to add $1$ to the this, with the result
                  $2^n-1 + lvert x rvert,$ which is the $n$-bit signed-magnitude representation of $x$ when $x < 0.$



                  In summary, the proposed method does indeed give correct results in all cases except when overflow occurs, and those are precisely the cases where it is not possible to represent the correct result in signed magnitude
                  (that is, those are the cases in which no algorithm can give the correct answer).






                  share|cite|improve this answer












                  Let's take a second look at the "book" method of adding $-5$ and $3$ in four-bit two's-complement arithmetic.



                  In four-bit two's-complement binary, $-5$ is represented by $1011$ and $3$ is represented by $0011.$ The sum is $1011 + 0011 = 1110,$ which is the four-bit two's-complement representation of $-2.$ And that's the answer: $-2.$



                  The only reason to do any further operations on the result is if you want to convert the result back to ordinary decimal notation and don't instantly recognize that $1110$ represents $-2.$
                  One way to do that is to take the two's complement of $1110,$ which is $0010.$ Taking the two's complement of a number changes the sign of the number in the two's-complement representation.
                  If reversing the sign of $1110$ gives us $0010,$ which represents $2,$ then what does $1110$ represent? It can only represent $-2.$
                  Again, that 's the answer: $-2.$




                  The rest of the question presents an interesting procedure for adding binary representations of integers. Instead of using two's-complement exclusively, however, this method begins with the two operands ($-5$ and $3$) in a four-bit signed-magnitude representation, and ends with the result ($-2$) in four-bit signed-magnitude representation.



                  The method has three major steps:



                  1. Convert the signed-magnitude representation to two's complement.

                  2. Add the two's-complement representations.

                  3. Convert the result from two's complement to signed magnitude.

                  The two's-complement addition is performed in the conventional way, so we merely have to check that the conversions from signed-magnitude to two's-complement and back again are correct.



                  Positive numbers are "converted" by leaving their bits completely unchanged.
                  Only negative numbers have their bits changed by the conversion.
                  In order to be able to reason about the conversion mathematically, I'll evaluate each four-bit binary number as an unsigned integer, although it can still also be interpreted as signed magnitude or two's complement.



                  If a four-bit signed-magnitude binary number represents $x,$ where $x < 0,$
                  then the bits of that representation have unsigned value
                  $8 - lvert xrvert.$ (The sign bit has place value $8,$ and the rightmost three bits have value $lvert xrvert.$)
                  The first step is to take the one's complement of the three magnitude bits,
                  which is equivalent to subtracting them from the three-bit number $111,$
                  that is, from $7.$ The sign bit remains unchanged, so after this step
                  we have a four-bit number with unsigned value
                  $8 + (7 - lvert xrvert) = 15 - lvert xrvert.$
                  The second step is to add $1.$ The resulting number has unsigned value
                  $(15 - lvert xrvert) + 1 = 16 - lvert xrvert = 16 + x,$
                  since $x < 0.$
                  And $16 + x$ is the unsigned value of the four-bit two's-complement representation of a number $x$ when $x < 0.$
                  Hence the conversion is always correct.



                  More generally, for an $n$-bit signed-magnitude representation of a
                  negative number $x,$ the
                  unsigned value of the signed-magnitude representation is
                  $2^n-1 + lvert xrvert,$ after taking the one's complement of the magnitude we have
                  $2^n-1 + (2^n-1 - 1 - lvert xrvert) = 2^n - 1 - lvert xrvert,$
                  and after adding $1$ we have
                  $(2^n - 1 - lvert xrvert) + 1 = 2^n - lvert xrvert = 2^n + x,$
                  the two's complement representation of $x.$ Since the signed-magnitude
                  representation can only represent a number $x$ if
                  $lvert xrvert leq 2^n-1 - 1,$ there is no possibility of overflow,
                  and this conversion will always work.



                  Note that if the signed-magnitude number is $-0$ (sign bit $1$ with magnitude $0$), the procedure above produces a number with all bits set to $1$ after the one's complement, and adding $1$ to this results in all bits set to $0.$
                  That is, this signed-magnitude representation is correctly converted to $0$ in two's complement.



                  To convert from two's complement to signed magnitude, again positive numbers are not changed, and only negative numbers require manipulation of their bits.
                  Clearly the procedure cannot work when $x = -2^n-1,$ which is the most negative possible number that can be represented in $n$-bit two's complement,
                  because the least number that can be represented in $n$-bit signed magnitude is $-(2^n-1 - 1).$
                  So let's assume that $-(2^n-1 - 1) leq x leq -1,$ that is, $x$ can be
                  any of the possible negative integers in $n$-bit two's-complement binary representation except $-2^n-1$
                  Then the $n$-bit two's-complement representation of $x$ is a binary number
                  with unsigned value
                  $2^n + x = 2^n - lvert x rvert geq 2^n-1 + (2^n-1 - lvert x rvert).$
                  That is, the rightmost $n-1$ bits have unsigned value
                  $2^n-1 - lvert x rvert.$
                  We take the one's complement of these bits,
                  $(2^n-1 - 1) - (2^n-1 - lvert x rvert) = lvert x rvert - 1,$
                  and put these to the right of the sign bit, so we have a binary number
                  with unsigned value $2^n-1 + lvert x rvert - 1.$
                  The next step is to add $1$ to the this, with the result
                  $2^n-1 + lvert x rvert,$ which is the $n$-bit signed-magnitude representation of $x$ when $x < 0.$



                  In summary, the proposed method does indeed give correct results in all cases except when overflow occurs, and those are precisely the cases where it is not possible to represent the correct result in signed magnitude
                  (that is, those are the cases in which no algorithm can give the correct answer).







                  share|cite|improve this answer












                  share|cite|improve this answer



                  share|cite|improve this answer










                  answered May 22 '17 at 3:44









                  David K

                  49k340109




                  49k340109



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f1331621%2fsigned-numbers-binary-addition%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      這個網誌中的熱門文章

                      How to combine Bézier curves to a surface?

                      Mutual Information Always Non-negative

                      Why am i infinitely getting the same tweet with the Twitter Search API?