Which arithmetic operations are the same on unsigned and two's complement signed numbers?Assembly: is numeric operations for signed different from that of unsigned?Is there a single instruction to add numbers in U2 format in assembly?Why doesn't C have unsigned floats?Why are signed and unsigned multiplication different instructions on x86(-64)?how does c compiler handle unsigned and signed integer? Why the assembly code for unsigned and signed arithmetic operation are the same?Difference between signed and unsigned on bitwise operationsWhy would we use addiu instead of addi?Which 2's complement integer operations can be used without zeroing high bits in the inputs, if only the low part of the result is wanted?Why does ARM distinguish between SDIV and UDIV but not with ADD, SUB and MUL?How to handle signed integers in ARM?What is “2's Complement”?Why prefer two's complement over sign-and-magnitude for signed numbers?two's complement, why the name “two”Performing arithmetic operations in binary using only bitwise operatorsHow to divide two negative numbers using the two's complement method?How to get the signed complement of a number?Is the two's complement always a signed number?How to add hex numbers using two's complement?Why does Javascript treat a number as a two's complementAre the bytes stored in 6502 memory signed or unsigned?

Why are MBA programs closing?

Are there any important biographies of nobodies?

Which languages would be most useful in Europe at the end of the 19th century?

How to safely destroy (a large quantity of) valid checks?

Someone whose aspirations exceed abilities or means

How do you say "homebrewer" in Spanish?

Traversing Oceania: A Cryptic Journey

Let M and N be single-digit integers. If the product 2M5 x 13N is divisible by 36, how many ordered pairs (M,N) are possible?

What ways have you found to get edits from non-LaTeX users?

Why can my keyboard only digest 6 keypresses at a time?

Who enforces MPAA rating adherence?

Can I utilise a baking stone to make crepes?

US doctor working in Tripoli wants me to open online account

Generate basis elements of the Steenrod algebra

How can I end combat quickly when the outcome is inevitable?

Why does the Mishnah use the terms poor person and homeowner when discussing carrying on Shabbat?

60s or 70s novel about Empire of Man making 1st contact with 1st discovered alien race

What is the maximum number of net attacks that one can make in a round?

How did old MS-DOS games utilize various graphic cards?

Cascading Switches. Will it affect performance?

sed + add word before string only if not exists

Overlapping String-Blocks

How to trick the reader into thinking they're following a redshirt instead of the protagonist?

Electricity free spaceship



Which arithmetic operations are the same on unsigned and two's complement signed numbers?


Assembly: is numeric operations for signed different from that of unsigned?Is there a single instruction to add numbers in U2 format in assembly?Why doesn't C have unsigned floats?Why are signed and unsigned multiplication different instructions on x86(-64)?how does c compiler handle unsigned and signed integer? Why the assembly code for unsigned and signed arithmetic operation are the same?Difference between signed and unsigned on bitwise operationsWhy would we use addiu instead of addi?Which 2's complement integer operations can be used without zeroing high bits in the inputs, if only the low part of the result is wanted?Why does ARM distinguish between SDIV and UDIV but not with ADD, SUB and MUL?How to handle signed integers in ARM?What is “2's Complement”?Why prefer two's complement over sign-and-magnitude for signed numbers?two's complement, why the name “two”Performing arithmetic operations in binary using only bitwise operatorsHow to divide two negative numbers using the two's complement method?How to get the signed complement of a number?Is the two's complement always a signed number?How to add hex numbers using two's complement?Why does Javascript treat a number as a two's complementAre the bytes stored in 6502 memory signed or unsigned?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








4















I'm designing a simple toy instruction set and accompanying emulator, and I'm trying to figure out what instructions to support. In the way of arithmetic, I currently have unsigned add, subtract, multiply, and divide. However, I can't seem to find a definitive answer to the following question: Which of the arithmetic operators need signed versions, and for which are the unsigned and two's complement signed versions equivalent?



So, for example, 1111 in two's complement is equal to -1. If you add 1 to it and pretend that it's an unsigned number , you get 0000, which is correct even when thinking of it as -1. However, does that hold for all numbers? And what about for the other three operations (subtraction, multiplication, division)?










share|improve this question
























  • Somewhat related: Which 2's complement integer operations can be used without zeroing high bits in the inputs, if only the low part of the result is wanted?

    – Peter Cordes
    Jul 9 '18 at 23:02

















4















I'm designing a simple toy instruction set and accompanying emulator, and I'm trying to figure out what instructions to support. In the way of arithmetic, I currently have unsigned add, subtract, multiply, and divide. However, I can't seem to find a definitive answer to the following question: Which of the arithmetic operators need signed versions, and for which are the unsigned and two's complement signed versions equivalent?



So, for example, 1111 in two's complement is equal to -1. If you add 1 to it and pretend that it's an unsigned number , you get 0000, which is correct even when thinking of it as -1. However, does that hold for all numbers? And what about for the other three operations (subtraction, multiplication, division)?










share|improve this question
























  • Somewhat related: Which 2's complement integer operations can be used without zeroing high bits in the inputs, if only the low part of the result is wanted?

    – Peter Cordes
    Jul 9 '18 at 23:02













4












4








4








I'm designing a simple toy instruction set and accompanying emulator, and I'm trying to figure out what instructions to support. In the way of arithmetic, I currently have unsigned add, subtract, multiply, and divide. However, I can't seem to find a definitive answer to the following question: Which of the arithmetic operators need signed versions, and for which are the unsigned and two's complement signed versions equivalent?



So, for example, 1111 in two's complement is equal to -1. If you add 1 to it and pretend that it's an unsigned number , you get 0000, which is correct even when thinking of it as -1. However, does that hold for all numbers? And what about for the other three operations (subtraction, multiplication, division)?










share|improve this question
















I'm designing a simple toy instruction set and accompanying emulator, and I'm trying to figure out what instructions to support. In the way of arithmetic, I currently have unsigned add, subtract, multiply, and divide. However, I can't seem to find a definitive answer to the following question: Which of the arithmetic operators need signed versions, and for which are the unsigned and two's complement signed versions equivalent?



So, for example, 1111 in two's complement is equal to -1. If you add 1 to it and pretend that it's an unsigned number , you get 0000, which is correct even when thinking of it as -1. However, does that hold for all numbers? And what about for the other three operations (subtraction, multiplication, division)?







binary twos-complement instructions instruction-set






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 14 '18 at 13:00









phuclv

17.1k957242




17.1k957242










asked Jan 31 '14 at 8:16









joshlfjoshlf

10.7k34763




10.7k34763












  • Somewhat related: Which 2's complement integer operations can be used without zeroing high bits in the inputs, if only the low part of the result is wanted?

    – Peter Cordes
    Jul 9 '18 at 23:02

















  • Somewhat related: Which 2's complement integer operations can be used without zeroing high bits in the inputs, if only the low part of the result is wanted?

    – Peter Cordes
    Jul 9 '18 at 23:02
















Somewhat related: Which 2's complement integer operations can be used without zeroing high bits in the inputs, if only the low part of the result is wanted?

– Peter Cordes
Jul 9 '18 at 23:02





Somewhat related: Which 2's complement integer operations can be used without zeroing high bits in the inputs, if only the low part of the result is wanted?

– Peter Cordes
Jul 9 '18 at 23:02












3 Answers
3






active

oldest

votes


















2














Add and subtract are the same for signed and unsigned 2s complement, assuming you're going to handle overflow/underflow in the normal way for most CPUs, i.e. just wrap around. Multiply and divide are different. So you only need one addition routine and one subtraction routine regardless of signedness, but you need separate signed and unsigned multiply and divide.






share|improve this answer




















  • 4





    Non-widening multiplication result is the same for both signed and unsigned types

    – phuclv
    Mar 29 '16 at 3:14


















2














Addition, subtraction and multiplication are the same provided:



  1. Your inputs and outputs are the same size

  2. Your behaviour on overflow is wraparound modulo 2n

Division is different.



Many instruction sets offer multiplication operations where the output is larger than the input, again these are different for signed and unsigned.



Furthermore if you are writing your emulator in C there are some misfeatures of the language that you need to be aware of.



  1. Overflow of signed arithmetic in C is undefined behaviour. To get reliable modulo 2n behaviour arithmetic must be performed using unsigned types.

  2. C will promote types smaller than int to int. Great care is needed to avoid such promotions (adding 0u or multiplying by 1u at the start of your calculation is one way).

  3. Conversion from unsigned types to signed types is implementation defined, the implementations i've seen do the sensible thing but there may be some that don't.





share|improve this answer






























    0














    All your operations need overflow checks, or they will return incorrect values in some cases. The unsigned versions of these checks are different from the signed ones, so you'll need to implement each routine separately.






    share|improve this answer


















    • 1





      Since this is an instruction set emulator I would expect that integer overflow/underflow would behave in the usual way, i.e. wrap around modulo 2^N, so addition and subtraction would be the same for both signed and unsigned.

      – Paul R
      Jan 31 '14 at 8:34











    • The way real instruction sets usually handle this is by having multiple flags that can be used to detect different types of overflow.

      – plugwash
      Mar 2 '18 at 13:23











    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%2f21475286%2fwhich-arithmetic-operations-are-the-same-on-unsigned-and-twos-complement-signed%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Add and subtract are the same for signed and unsigned 2s complement, assuming you're going to handle overflow/underflow in the normal way for most CPUs, i.e. just wrap around. Multiply and divide are different. So you only need one addition routine and one subtraction routine regardless of signedness, but you need separate signed and unsigned multiply and divide.






    share|improve this answer




















    • 4





      Non-widening multiplication result is the same for both signed and unsigned types

      – phuclv
      Mar 29 '16 at 3:14















    2














    Add and subtract are the same for signed and unsigned 2s complement, assuming you're going to handle overflow/underflow in the normal way for most CPUs, i.e. just wrap around. Multiply and divide are different. So you only need one addition routine and one subtraction routine regardless of signedness, but you need separate signed and unsigned multiply and divide.






    share|improve this answer




















    • 4





      Non-widening multiplication result is the same for both signed and unsigned types

      – phuclv
      Mar 29 '16 at 3:14













    2












    2








    2







    Add and subtract are the same for signed and unsigned 2s complement, assuming you're going to handle overflow/underflow in the normal way for most CPUs, i.e. just wrap around. Multiply and divide are different. So you only need one addition routine and one subtraction routine regardless of signedness, but you need separate signed and unsigned multiply and divide.






    share|improve this answer















    Add and subtract are the same for signed and unsigned 2s complement, assuming you're going to handle overflow/underflow in the normal way for most CPUs, i.e. just wrap around. Multiply and divide are different. So you only need one addition routine and one subtraction routine regardless of signedness, but you need separate signed and unsigned multiply and divide.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 31 '14 at 8:33

























    answered Jan 31 '14 at 8:19









    Paul RPaul R

    179k25306467




    179k25306467







    • 4





      Non-widening multiplication result is the same for both signed and unsigned types

      – phuclv
      Mar 29 '16 at 3:14












    • 4





      Non-widening multiplication result is the same for both signed and unsigned types

      – phuclv
      Mar 29 '16 at 3:14







    4




    4





    Non-widening multiplication result is the same for both signed and unsigned types

    – phuclv
    Mar 29 '16 at 3:14





    Non-widening multiplication result is the same for both signed and unsigned types

    – phuclv
    Mar 29 '16 at 3:14













    2














    Addition, subtraction and multiplication are the same provided:



    1. Your inputs and outputs are the same size

    2. Your behaviour on overflow is wraparound modulo 2n

    Division is different.



    Many instruction sets offer multiplication operations where the output is larger than the input, again these are different for signed and unsigned.



    Furthermore if you are writing your emulator in C there are some misfeatures of the language that you need to be aware of.



    1. Overflow of signed arithmetic in C is undefined behaviour. To get reliable modulo 2n behaviour arithmetic must be performed using unsigned types.

    2. C will promote types smaller than int to int. Great care is needed to avoid such promotions (adding 0u or multiplying by 1u at the start of your calculation is one way).

    3. Conversion from unsigned types to signed types is implementation defined, the implementations i've seen do the sensible thing but there may be some that don't.





    share|improve this answer



























      2














      Addition, subtraction and multiplication are the same provided:



      1. Your inputs and outputs are the same size

      2. Your behaviour on overflow is wraparound modulo 2n

      Division is different.



      Many instruction sets offer multiplication operations where the output is larger than the input, again these are different for signed and unsigned.



      Furthermore if you are writing your emulator in C there are some misfeatures of the language that you need to be aware of.



      1. Overflow of signed arithmetic in C is undefined behaviour. To get reliable modulo 2n behaviour arithmetic must be performed using unsigned types.

      2. C will promote types smaller than int to int. Great care is needed to avoid such promotions (adding 0u or multiplying by 1u at the start of your calculation is one way).

      3. Conversion from unsigned types to signed types is implementation defined, the implementations i've seen do the sensible thing but there may be some that don't.





      share|improve this answer

























        2












        2








        2







        Addition, subtraction and multiplication are the same provided:



        1. Your inputs and outputs are the same size

        2. Your behaviour on overflow is wraparound modulo 2n

        Division is different.



        Many instruction sets offer multiplication operations where the output is larger than the input, again these are different for signed and unsigned.



        Furthermore if you are writing your emulator in C there are some misfeatures of the language that you need to be aware of.



        1. Overflow of signed arithmetic in C is undefined behaviour. To get reliable modulo 2n behaviour arithmetic must be performed using unsigned types.

        2. C will promote types smaller than int to int. Great care is needed to avoid such promotions (adding 0u or multiplying by 1u at the start of your calculation is one way).

        3. Conversion from unsigned types to signed types is implementation defined, the implementations i've seen do the sensible thing but there may be some that don't.





        share|improve this answer













        Addition, subtraction and multiplication are the same provided:



        1. Your inputs and outputs are the same size

        2. Your behaviour on overflow is wraparound modulo 2n

        Division is different.



        Many instruction sets offer multiplication operations where the output is larger than the input, again these are different for signed and unsigned.



        Furthermore if you are writing your emulator in C there are some misfeatures of the language that you need to be aware of.



        1. Overflow of signed arithmetic in C is undefined behaviour. To get reliable modulo 2n behaviour arithmetic must be performed using unsigned types.

        2. C will promote types smaller than int to int. Great care is needed to avoid such promotions (adding 0u or multiplying by 1u at the start of your calculation is one way).

        3. Conversion from unsigned types to signed types is implementation defined, the implementations i've seen do the sensible thing but there may be some that don't.






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 2 '18 at 13:37









        plugwashplugwash

        4,7361327




        4,7361327





















            0














            All your operations need overflow checks, or they will return incorrect values in some cases. The unsigned versions of these checks are different from the signed ones, so you'll need to implement each routine separately.






            share|improve this answer


















            • 1





              Since this is an instruction set emulator I would expect that integer overflow/underflow would behave in the usual way, i.e. wrap around modulo 2^N, so addition and subtraction would be the same for both signed and unsigned.

              – Paul R
              Jan 31 '14 at 8:34











            • The way real instruction sets usually handle this is by having multiple flags that can be used to detect different types of overflow.

              – plugwash
              Mar 2 '18 at 13:23















            0














            All your operations need overflow checks, or they will return incorrect values in some cases. The unsigned versions of these checks are different from the signed ones, so you'll need to implement each routine separately.






            share|improve this answer


















            • 1





              Since this is an instruction set emulator I would expect that integer overflow/underflow would behave in the usual way, i.e. wrap around modulo 2^N, so addition and subtraction would be the same for both signed and unsigned.

              – Paul R
              Jan 31 '14 at 8:34











            • The way real instruction sets usually handle this is by having multiple flags that can be used to detect different types of overflow.

              – plugwash
              Mar 2 '18 at 13:23













            0












            0








            0







            All your operations need overflow checks, or they will return incorrect values in some cases. The unsigned versions of these checks are different from the signed ones, so you'll need to implement each routine separately.






            share|improve this answer













            All your operations need overflow checks, or they will return incorrect values in some cases. The unsigned versions of these checks are different from the signed ones, so you'll need to implement each routine separately.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 31 '14 at 8:20









            Guntram BlohmGuntram Blohm

            8,50321426




            8,50321426







            • 1





              Since this is an instruction set emulator I would expect that integer overflow/underflow would behave in the usual way, i.e. wrap around modulo 2^N, so addition and subtraction would be the same for both signed and unsigned.

              – Paul R
              Jan 31 '14 at 8:34











            • The way real instruction sets usually handle this is by having multiple flags that can be used to detect different types of overflow.

              – plugwash
              Mar 2 '18 at 13:23












            • 1





              Since this is an instruction set emulator I would expect that integer overflow/underflow would behave in the usual way, i.e. wrap around modulo 2^N, so addition and subtraction would be the same for both signed and unsigned.

              – Paul R
              Jan 31 '14 at 8:34











            • The way real instruction sets usually handle this is by having multiple flags that can be used to detect different types of overflow.

              – plugwash
              Mar 2 '18 at 13:23







            1




            1





            Since this is an instruction set emulator I would expect that integer overflow/underflow would behave in the usual way, i.e. wrap around modulo 2^N, so addition and subtraction would be the same for both signed and unsigned.

            – Paul R
            Jan 31 '14 at 8:34





            Since this is an instruction set emulator I would expect that integer overflow/underflow would behave in the usual way, i.e. wrap around modulo 2^N, so addition and subtraction would be the same for both signed and unsigned.

            – Paul R
            Jan 31 '14 at 8:34













            The way real instruction sets usually handle this is by having multiple flags that can be used to detect different types of overflow.

            – plugwash
            Mar 2 '18 at 13:23





            The way real instruction sets usually handle this is by having multiple flags that can be used to detect different types of overflow.

            – plugwash
            Mar 2 '18 at 13:23

















            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%2f21475286%2fwhich-arithmetic-operations-are-the-same-on-unsigned-and-twos-complement-signed%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

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현