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;
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
add a comment |
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
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
add a comment |
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
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
binary twos-complement instructions instruction-set
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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.
4
Non-widening multiplication result is the same for both signed and unsigned types
– phuclv
Mar 29 '16 at 3:14
add a comment |
Addition, subtraction and multiplication are the same provided:
- Your inputs and outputs are the same size
- 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.
- Overflow of signed arithmetic in C is undefined behaviour. To get reliable modulo 2n behaviour arithmetic must be performed using unsigned types.
- 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).
- 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.
add a comment |
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.
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
4
Non-widening multiplication result is the same for both signed and unsigned types
– phuclv
Mar 29 '16 at 3:14
add a comment |
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.
4
Non-widening multiplication result is the same for both signed and unsigned types
– phuclv
Mar 29 '16 at 3:14
add a comment |
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.
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.
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
add a comment |
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
add a comment |
Addition, subtraction and multiplication are the same provided:
- Your inputs and outputs are the same size
- 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.
- Overflow of signed arithmetic in C is undefined behaviour. To get reliable modulo 2n behaviour arithmetic must be performed using unsigned types.
- 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).
- 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.
add a comment |
Addition, subtraction and multiplication are the same provided:
- Your inputs and outputs are the same size
- 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.
- Overflow of signed arithmetic in C is undefined behaviour. To get reliable modulo 2n behaviour arithmetic must be performed using unsigned types.
- 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).
- 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.
add a comment |
Addition, subtraction and multiplication are the same provided:
- Your inputs and outputs are the same size
- 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.
- Overflow of signed arithmetic in C is undefined behaviour. To get reliable modulo 2n behaviour arithmetic must be performed using unsigned types.
- 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).
- 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.
Addition, subtraction and multiplication are the same provided:
- Your inputs and outputs are the same size
- 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.
- Overflow of signed arithmetic in C is undefined behaviour. To get reliable modulo 2n behaviour arithmetic must be performed using unsigned types.
- 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).
- 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.
answered Mar 2 '18 at 13:37
plugwashplugwash
4,7361327
4,7361327
add a comment |
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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