How to detect 0 subtract -128 overflow in 8 bits environment?










0















The question confuses me a long time. How to detect overflow in such situation which 0 subtracts (-128) in
8 bits environment.



In class, my teacher teach me a method
to detect overflow, the method is as below:



method worked



But it doesn't work in such situation:



method not worked



Above situation computer can detect overflow. But I don't know how computer does it.










share|improve this question




























    0















    The question confuses me a long time. How to detect overflow in such situation which 0 subtracts (-128) in
    8 bits environment.



    In class, my teacher teach me a method
    to detect overflow, the method is as below:



    method worked



    But it doesn't work in such situation:



    method not worked



    Above situation computer can detect overflow. But I don't know how computer does it.










    share|improve this question


























      0












      0








      0








      The question confuses me a long time. How to detect overflow in such situation which 0 subtracts (-128) in
      8 bits environment.



      In class, my teacher teach me a method
      to detect overflow, the method is as below:



      method worked



      But it doesn't work in such situation:



      method not worked



      Above situation computer can detect overflow. But I don't know how computer does it.










      share|improve this question
















      The question confuses me a long time. How to detect overflow in such situation which 0 subtracts (-128) in
      8 bits environment.



      In class, my teacher teach me a method
      to detect overflow, the method is as below:



      method worked



      But it doesn't work in such situation:



      method not worked



      Above situation computer can detect overflow. But I don't know how computer does it.







      assembly digits






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 8:28









      Foo

      1




      1










      asked Nov 13 '18 at 8:12









      Terry HuangTerry Huang

      1




      1






















          1 Answer
          1






          active

          oldest

          votes


















          1














          You've got an error in your calculation.

          Subtracting two numbers in two's complement is not how you've depicted it in the second image.



          The image shows addition, not subtraction, and in subtraction you use borrow not carry.

          The borrow is where you've made your mistake.



           10000000 (borrow)
          00000000 (0)
          - 10000000 (-128)
          ----------
          10000000 (-128)


          As you can see the first two bits of the borrow are different hence 1 XOR 0 = 1 yields overflow.



          See Wikipedia for more information.



          Addendum



          To clarify why your assumption of that an overflow has not occurred, when infact it has, in your addition calculation in the second image, is wrong:



           00000000 (carry)
          00000000 (0)
          + 10000000 (-128)
          ----------
          10000000 (-128)


          Since 0 + -128 = -128 and 0 XOR 0 = 0 hence no overflow.

          In this case the addition doesn't cross the -128 and 127 boundary.



          Let's look at a representation of two's complement with only 4-bits.



          Two's complement number circle



          -128 would be represented by -8.

          If you go from 0 to -8 counter-clockwise (for subtraction) you'll see that the boundary (depicted by the red line) is not crossed, hence no overflow.



          If we take your first example which would be 7 + 1 and go clockwise (addition) from 7 you'll end up at -8 and you have crossed the boundary, hence overflowed.



          What you thought you did in your second image was 0 + 128. You went from 0 to -128 clockwise (addition). This crossed the boundary but since 128 cannot be represented in 8-bit two's complement the assumption and calculation was wrong.



          You assumed 0 + 128 was the same as 0 - (-128) which it clearly isn't since as said above 128 cannot be represented in 8-bit two's complement.






          share|improve this answer

























          • Thank for your answer! I think my subtraction way is wrong. I will try more case~

            – Terry Huang
            Nov 13 '18 at 13:47










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          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',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276523%2fhow-to-detect-0-subtract-128-overflow-in-8-bits-environment%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You've got an error in your calculation.

          Subtracting two numbers in two's complement is not how you've depicted it in the second image.



          The image shows addition, not subtraction, and in subtraction you use borrow not carry.

          The borrow is where you've made your mistake.



           10000000 (borrow)
          00000000 (0)
          - 10000000 (-128)
          ----------
          10000000 (-128)


          As you can see the first two bits of the borrow are different hence 1 XOR 0 = 1 yields overflow.



          See Wikipedia for more information.



          Addendum



          To clarify why your assumption of that an overflow has not occurred, when infact it has, in your addition calculation in the second image, is wrong:



           00000000 (carry)
          00000000 (0)
          + 10000000 (-128)
          ----------
          10000000 (-128)


          Since 0 + -128 = -128 and 0 XOR 0 = 0 hence no overflow.

          In this case the addition doesn't cross the -128 and 127 boundary.



          Let's look at a representation of two's complement with only 4-bits.



          Two's complement number circle



          -128 would be represented by -8.

          If you go from 0 to -8 counter-clockwise (for subtraction) you'll see that the boundary (depicted by the red line) is not crossed, hence no overflow.



          If we take your first example which would be 7 + 1 and go clockwise (addition) from 7 you'll end up at -8 and you have crossed the boundary, hence overflowed.



          What you thought you did in your second image was 0 + 128. You went from 0 to -128 clockwise (addition). This crossed the boundary but since 128 cannot be represented in 8-bit two's complement the assumption and calculation was wrong.



          You assumed 0 + 128 was the same as 0 - (-128) which it clearly isn't since as said above 128 cannot be represented in 8-bit two's complement.






          share|improve this answer

























          • Thank for your answer! I think my subtraction way is wrong. I will try more case~

            – Terry Huang
            Nov 13 '18 at 13:47















          1














          You've got an error in your calculation.

          Subtracting two numbers in two's complement is not how you've depicted it in the second image.



          The image shows addition, not subtraction, and in subtraction you use borrow not carry.

          The borrow is where you've made your mistake.



           10000000 (borrow)
          00000000 (0)
          - 10000000 (-128)
          ----------
          10000000 (-128)


          As you can see the first two bits of the borrow are different hence 1 XOR 0 = 1 yields overflow.



          See Wikipedia for more information.



          Addendum



          To clarify why your assumption of that an overflow has not occurred, when infact it has, in your addition calculation in the second image, is wrong:



           00000000 (carry)
          00000000 (0)
          + 10000000 (-128)
          ----------
          10000000 (-128)


          Since 0 + -128 = -128 and 0 XOR 0 = 0 hence no overflow.

          In this case the addition doesn't cross the -128 and 127 boundary.



          Let's look at a representation of two's complement with only 4-bits.



          Two's complement number circle



          -128 would be represented by -8.

          If you go from 0 to -8 counter-clockwise (for subtraction) you'll see that the boundary (depicted by the red line) is not crossed, hence no overflow.



          If we take your first example which would be 7 + 1 and go clockwise (addition) from 7 you'll end up at -8 and you have crossed the boundary, hence overflowed.



          What you thought you did in your second image was 0 + 128. You went from 0 to -128 clockwise (addition). This crossed the boundary but since 128 cannot be represented in 8-bit two's complement the assumption and calculation was wrong.



          You assumed 0 + 128 was the same as 0 - (-128) which it clearly isn't since as said above 128 cannot be represented in 8-bit two's complement.






          share|improve this answer

























          • Thank for your answer! I think my subtraction way is wrong. I will try more case~

            – Terry Huang
            Nov 13 '18 at 13:47













          1












          1








          1







          You've got an error in your calculation.

          Subtracting two numbers in two's complement is not how you've depicted it in the second image.



          The image shows addition, not subtraction, and in subtraction you use borrow not carry.

          The borrow is where you've made your mistake.



           10000000 (borrow)
          00000000 (0)
          - 10000000 (-128)
          ----------
          10000000 (-128)


          As you can see the first two bits of the borrow are different hence 1 XOR 0 = 1 yields overflow.



          See Wikipedia for more information.



          Addendum



          To clarify why your assumption of that an overflow has not occurred, when infact it has, in your addition calculation in the second image, is wrong:



           00000000 (carry)
          00000000 (0)
          + 10000000 (-128)
          ----------
          10000000 (-128)


          Since 0 + -128 = -128 and 0 XOR 0 = 0 hence no overflow.

          In this case the addition doesn't cross the -128 and 127 boundary.



          Let's look at a representation of two's complement with only 4-bits.



          Two's complement number circle



          -128 would be represented by -8.

          If you go from 0 to -8 counter-clockwise (for subtraction) you'll see that the boundary (depicted by the red line) is not crossed, hence no overflow.



          If we take your first example which would be 7 + 1 and go clockwise (addition) from 7 you'll end up at -8 and you have crossed the boundary, hence overflowed.



          What you thought you did in your second image was 0 + 128. You went from 0 to -128 clockwise (addition). This crossed the boundary but since 128 cannot be represented in 8-bit two's complement the assumption and calculation was wrong.



          You assumed 0 + 128 was the same as 0 - (-128) which it clearly isn't since as said above 128 cannot be represented in 8-bit two's complement.






          share|improve this answer















          You've got an error in your calculation.

          Subtracting two numbers in two's complement is not how you've depicted it in the second image.



          The image shows addition, not subtraction, and in subtraction you use borrow not carry.

          The borrow is where you've made your mistake.



           10000000 (borrow)
          00000000 (0)
          - 10000000 (-128)
          ----------
          10000000 (-128)


          As you can see the first two bits of the borrow are different hence 1 XOR 0 = 1 yields overflow.



          See Wikipedia for more information.



          Addendum



          To clarify why your assumption of that an overflow has not occurred, when infact it has, in your addition calculation in the second image, is wrong:



           00000000 (carry)
          00000000 (0)
          + 10000000 (-128)
          ----------
          10000000 (-128)


          Since 0 + -128 = -128 and 0 XOR 0 = 0 hence no overflow.

          In this case the addition doesn't cross the -128 and 127 boundary.



          Let's look at a representation of two's complement with only 4-bits.



          Two's complement number circle



          -128 would be represented by -8.

          If you go from 0 to -8 counter-clockwise (for subtraction) you'll see that the boundary (depicted by the red line) is not crossed, hence no overflow.



          If we take your first example which would be 7 + 1 and go clockwise (addition) from 7 you'll end up at -8 and you have crossed the boundary, hence overflowed.



          What you thought you did in your second image was 0 + 128. You went from 0 to -128 clockwise (addition). This crossed the boundary but since 128 cannot be represented in 8-bit two's complement the assumption and calculation was wrong.



          You assumed 0 + 128 was the same as 0 - (-128) which it clearly isn't since as said above 128 cannot be represented in 8-bit two's complement.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 10:50

























          answered Nov 13 '18 at 9:21









          Sani Singh HuttunenSani Singh Huttunen

          18.7k35365




          18.7k35365












          • Thank for your answer! I think my subtraction way is wrong. I will try more case~

            – Terry Huang
            Nov 13 '18 at 13:47

















          • Thank for your answer! I think my subtraction way is wrong. I will try more case~

            – Terry Huang
            Nov 13 '18 at 13:47
















          Thank for your answer! I think my subtraction way is wrong. I will try more case~

          – Terry Huang
          Nov 13 '18 at 13:47





          Thank for your answer! I think my subtraction way is wrong. I will try more case~

          – Terry Huang
          Nov 13 '18 at 13:47

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276523%2fhow-to-detect-0-subtract-128-overflow-in-8-bits-environment%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo