Unsigned arithmetic in C++











up vote
11
down vote

favorite
2












I just observed a strange phenomenon when doing unsigned arithmetic. It's expected that b and -a have the same number 4294967286 due to wraparound, but the actual output for b and -a is -10 and 4294967286 respectively. Could anyone help give a hint?



#include <iostream>

int main() {
unsigned int a = 10;
int b = -a;
std::cout << b << ", " << -a << std::endl;
}


https://repl.it/repls/ExpertDrabOrganization










share|improve this question




















  • 2




    Why do you expect that an int would have the value 4294967286, which is much larger than the maximum int?
    – molbdnilo
    Nov 20 at 8:15








  • 1




    int b = -a; -> unsigned int b = -a;
    – Paul R
    Nov 20 at 8:18






  • 1




    @molbdnilo My fault. I forgot that b overflows, too.
    – Zhe Chen
    Nov 20 at 8:20

















up vote
11
down vote

favorite
2












I just observed a strange phenomenon when doing unsigned arithmetic. It's expected that b and -a have the same number 4294967286 due to wraparound, but the actual output for b and -a is -10 and 4294967286 respectively. Could anyone help give a hint?



#include <iostream>

int main() {
unsigned int a = 10;
int b = -a;
std::cout << b << ", " << -a << std::endl;
}


https://repl.it/repls/ExpertDrabOrganization










share|improve this question




















  • 2




    Why do you expect that an int would have the value 4294967286, which is much larger than the maximum int?
    – molbdnilo
    Nov 20 at 8:15








  • 1




    int b = -a; -> unsigned int b = -a;
    – Paul R
    Nov 20 at 8:18






  • 1




    @molbdnilo My fault. I forgot that b overflows, too.
    – Zhe Chen
    Nov 20 at 8:20















up vote
11
down vote

favorite
2









up vote
11
down vote

favorite
2






2





I just observed a strange phenomenon when doing unsigned arithmetic. It's expected that b and -a have the same number 4294967286 due to wraparound, but the actual output for b and -a is -10 and 4294967286 respectively. Could anyone help give a hint?



#include <iostream>

int main() {
unsigned int a = 10;
int b = -a;
std::cout << b << ", " << -a << std::endl;
}


https://repl.it/repls/ExpertDrabOrganization










share|improve this question















I just observed a strange phenomenon when doing unsigned arithmetic. It's expected that b and -a have the same number 4294967286 due to wraparound, but the actual output for b and -a is -10 and 4294967286 respectively. Could anyone help give a hint?



#include <iostream>

int main() {
unsigned int a = 10;
int b = -a;
std::cout << b << ", " << -a << std::endl;
}


https://repl.it/repls/ExpertDrabOrganization







c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 8:14

























asked Nov 20 at 8:10









Zhe Chen

1,42331528




1,42331528








  • 2




    Why do you expect that an int would have the value 4294967286, which is much larger than the maximum int?
    – molbdnilo
    Nov 20 at 8:15








  • 1




    int b = -a; -> unsigned int b = -a;
    – Paul R
    Nov 20 at 8:18






  • 1




    @molbdnilo My fault. I forgot that b overflows, too.
    – Zhe Chen
    Nov 20 at 8:20
















  • 2




    Why do you expect that an int would have the value 4294967286, which is much larger than the maximum int?
    – molbdnilo
    Nov 20 at 8:15








  • 1




    int b = -a; -> unsigned int b = -a;
    – Paul R
    Nov 20 at 8:18






  • 1




    @molbdnilo My fault. I forgot that b overflows, too.
    – Zhe Chen
    Nov 20 at 8:20










2




2




Why do you expect that an int would have the value 4294967286, which is much larger than the maximum int?
– molbdnilo
Nov 20 at 8:15






Why do you expect that an int would have the value 4294967286, which is much larger than the maximum int?
– molbdnilo
Nov 20 at 8:15






1




1




int b = -a; -> unsigned int b = -a;
– Paul R
Nov 20 at 8:18




int b = -a; -> unsigned int b = -a;
– Paul R
Nov 20 at 8:18




1




1




@molbdnilo My fault. I forgot that b overflows, too.
– Zhe Chen
Nov 20 at 8:20






@molbdnilo My fault. I forgot that b overflows, too.
– Zhe Chen
Nov 20 at 8:20














1 Answer
1






active

oldest

votes

















up vote
17
down vote



accepted










-a is evaluated in unsigned arithmetic, and will be a number larger than std::numeric_limits<int>::max(). The unary operator - when applied to an unsigned type acts more like a modulus operator.



Therefore the behaviour of your program is implementation defined due to an out-of-range assignment to an int.






share|improve this answer



















  • 3




    implementation defined - I.e. look at the docs, and shout at your implementer if they don't do what they promise.
    – StoryTeller
    Nov 20 at 8:39












  • @StoryTeller: Since which flashy new standard?
    – Bathsheba
    Nov 20 at 8:40






  • 1




    @StoryTeller It was implementation-defined in C++03 too.
    – Angew
    Nov 20 at 8:52






  • 2




    @Angew - Interestingly enough, it's implementation defined in a C99 draft I have lying around. It would seem that a conversion was always with implementation defined semantics. Overflow in arithmetic with signed types is UB, however, wherever I checked. Curious.
    – StoryTeller
    Nov 20 at 9:03








  • 1




    gcc implementation defined behavior: The value is reduced module 2^N. So the same for clang. For MSVC this behavior is not ensured: docs.microsoft.com/en-us/cpp/c-language/…
    – Oliv
    Nov 20 at 9:44











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',
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%2f53388686%2funsigned-arithmetic-in-c%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








up vote
17
down vote



accepted










-a is evaluated in unsigned arithmetic, and will be a number larger than std::numeric_limits<int>::max(). The unary operator - when applied to an unsigned type acts more like a modulus operator.



Therefore the behaviour of your program is implementation defined due to an out-of-range assignment to an int.






share|improve this answer



















  • 3




    implementation defined - I.e. look at the docs, and shout at your implementer if they don't do what they promise.
    – StoryTeller
    Nov 20 at 8:39












  • @StoryTeller: Since which flashy new standard?
    – Bathsheba
    Nov 20 at 8:40






  • 1




    @StoryTeller It was implementation-defined in C++03 too.
    – Angew
    Nov 20 at 8:52






  • 2




    @Angew - Interestingly enough, it's implementation defined in a C99 draft I have lying around. It would seem that a conversion was always with implementation defined semantics. Overflow in arithmetic with signed types is UB, however, wherever I checked. Curious.
    – StoryTeller
    Nov 20 at 9:03








  • 1




    gcc implementation defined behavior: The value is reduced module 2^N. So the same for clang. For MSVC this behavior is not ensured: docs.microsoft.com/en-us/cpp/c-language/…
    – Oliv
    Nov 20 at 9:44















up vote
17
down vote



accepted










-a is evaluated in unsigned arithmetic, and will be a number larger than std::numeric_limits<int>::max(). The unary operator - when applied to an unsigned type acts more like a modulus operator.



Therefore the behaviour of your program is implementation defined due to an out-of-range assignment to an int.






share|improve this answer



















  • 3




    implementation defined - I.e. look at the docs, and shout at your implementer if they don't do what they promise.
    – StoryTeller
    Nov 20 at 8:39












  • @StoryTeller: Since which flashy new standard?
    – Bathsheba
    Nov 20 at 8:40






  • 1




    @StoryTeller It was implementation-defined in C++03 too.
    – Angew
    Nov 20 at 8:52






  • 2




    @Angew - Interestingly enough, it's implementation defined in a C99 draft I have lying around. It would seem that a conversion was always with implementation defined semantics. Overflow in arithmetic with signed types is UB, however, wherever I checked. Curious.
    – StoryTeller
    Nov 20 at 9:03








  • 1




    gcc implementation defined behavior: The value is reduced module 2^N. So the same for clang. For MSVC this behavior is not ensured: docs.microsoft.com/en-us/cpp/c-language/…
    – Oliv
    Nov 20 at 9:44













up vote
17
down vote



accepted







up vote
17
down vote



accepted






-a is evaluated in unsigned arithmetic, and will be a number larger than std::numeric_limits<int>::max(). The unary operator - when applied to an unsigned type acts more like a modulus operator.



Therefore the behaviour of your program is implementation defined due to an out-of-range assignment to an int.






share|improve this answer














-a is evaluated in unsigned arithmetic, and will be a number larger than std::numeric_limits<int>::max(). The unary operator - when applied to an unsigned type acts more like a modulus operator.



Therefore the behaviour of your program is implementation defined due to an out-of-range assignment to an int.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 13:23









Bakudan

13.3k84264




13.3k84264










answered Nov 20 at 8:17









Bathsheba

173k27244366




173k27244366








  • 3




    implementation defined - I.e. look at the docs, and shout at your implementer if they don't do what they promise.
    – StoryTeller
    Nov 20 at 8:39












  • @StoryTeller: Since which flashy new standard?
    – Bathsheba
    Nov 20 at 8:40






  • 1




    @StoryTeller It was implementation-defined in C++03 too.
    – Angew
    Nov 20 at 8:52






  • 2




    @Angew - Interestingly enough, it's implementation defined in a C99 draft I have lying around. It would seem that a conversion was always with implementation defined semantics. Overflow in arithmetic with signed types is UB, however, wherever I checked. Curious.
    – StoryTeller
    Nov 20 at 9:03








  • 1




    gcc implementation defined behavior: The value is reduced module 2^N. So the same for clang. For MSVC this behavior is not ensured: docs.microsoft.com/en-us/cpp/c-language/…
    – Oliv
    Nov 20 at 9:44














  • 3




    implementation defined - I.e. look at the docs, and shout at your implementer if they don't do what they promise.
    – StoryTeller
    Nov 20 at 8:39












  • @StoryTeller: Since which flashy new standard?
    – Bathsheba
    Nov 20 at 8:40






  • 1




    @StoryTeller It was implementation-defined in C++03 too.
    – Angew
    Nov 20 at 8:52






  • 2




    @Angew - Interestingly enough, it's implementation defined in a C99 draft I have lying around. It would seem that a conversion was always with implementation defined semantics. Overflow in arithmetic with signed types is UB, however, wherever I checked. Curious.
    – StoryTeller
    Nov 20 at 9:03








  • 1




    gcc implementation defined behavior: The value is reduced module 2^N. So the same for clang. For MSVC this behavior is not ensured: docs.microsoft.com/en-us/cpp/c-language/…
    – Oliv
    Nov 20 at 9:44








3




3




implementation defined - I.e. look at the docs, and shout at your implementer if they don't do what they promise.
– StoryTeller
Nov 20 at 8:39






implementation defined - I.e. look at the docs, and shout at your implementer if they don't do what they promise.
– StoryTeller
Nov 20 at 8:39














@StoryTeller: Since which flashy new standard?
– Bathsheba
Nov 20 at 8:40




@StoryTeller: Since which flashy new standard?
– Bathsheba
Nov 20 at 8:40




1




1




@StoryTeller It was implementation-defined in C++03 too.
– Angew
Nov 20 at 8:52




@StoryTeller It was implementation-defined in C++03 too.
– Angew
Nov 20 at 8:52




2




2




@Angew - Interestingly enough, it's implementation defined in a C99 draft I have lying around. It would seem that a conversion was always with implementation defined semantics. Overflow in arithmetic with signed types is UB, however, wherever I checked. Curious.
– StoryTeller
Nov 20 at 9:03






@Angew - Interestingly enough, it's implementation defined in a C99 draft I have lying around. It would seem that a conversion was always with implementation defined semantics. Overflow in arithmetic with signed types is UB, however, wherever I checked. Curious.
– StoryTeller
Nov 20 at 9:03






1




1




gcc implementation defined behavior: The value is reduced module 2^N. So the same for clang. For MSVC this behavior is not ensured: docs.microsoft.com/en-us/cpp/c-language/…
– Oliv
Nov 20 at 9:44




gcc implementation defined behavior: The value is reduced module 2^N. So the same for clang. For MSVC this behavior is not ensured: docs.microsoft.com/en-us/cpp/c-language/…
– Oliv
Nov 20 at 9:44


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53388686%2funsigned-arithmetic-in-c%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

AnyDesk - Fatal Program Failure

How to calibrate 16:9 built-in touch-screen to a 4:3 resolution?

QoS: MAC-Priority for clients behind a repeater