Unsigned arithmetic in C++
up vote
11
down vote
favorite
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++
add a comment |
up vote
11
down vote
favorite
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++
2
Why do you expect that anint
would have the value 4294967286, which is much larger than the maximumint
?
– molbdnilo
2 days ago
1
int b = -a;
->unsigned int b = -a;
– Paul R
2 days ago
1
@molbdnilo My fault. I forgot thatb
overflows, too.
– Zhe Chen
2 days ago
add a comment |
up vote
11
down vote
favorite
up vote
11
down vote
favorite
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++
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++
c++
edited 2 days ago
asked 2 days ago
Zhe Chen
1,42331528
1,42331528
2
Why do you expect that anint
would have the value 4294967286, which is much larger than the maximumint
?
– molbdnilo
2 days ago
1
int b = -a;
->unsigned int b = -a;
– Paul R
2 days ago
1
@molbdnilo My fault. I forgot thatb
overflows, too.
– Zhe Chen
2 days ago
add a comment |
2
Why do you expect that anint
would have the value 4294967286, which is much larger than the maximumint
?
– molbdnilo
2 days ago
1
int b = -a;
->unsigned int b = -a;
– Paul R
2 days ago
1
@molbdnilo My fault. I forgot thatb
overflows, too.
– Zhe Chen
2 days ago
2
2
Why do you expect that an
int
would have the value 4294967286, which is much larger than the maximum int
?– molbdnilo
2 days ago
Why do you expect that an
int
would have the value 4294967286, which is much larger than the maximum int
?– molbdnilo
2 days ago
1
1
int b = -a;
-> unsigned int b = -a;
– Paul R
2 days ago
int b = -a;
-> unsigned int b = -a;
– Paul R
2 days ago
1
1
@molbdnilo My fault. I forgot that
b
overflows, too.– Zhe Chen
2 days ago
@molbdnilo My fault. I forgot that
b
overflows, too.– Zhe Chen
2 days ago
add a comment |
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
.
3
implementation defined - I.e. look at the docs, and shout at your implementer if they don't do what they promise.
– StoryTeller
2 days ago
@StoryTeller: Since which flashy new standard?
– Bathsheba
2 days ago
1
@StoryTeller It was implementation-defined in C++03 too.
– Angew
2 days ago
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
2 days ago
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
2 days ago
|
show 3 more comments
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
.
3
implementation defined - I.e. look at the docs, and shout at your implementer if they don't do what they promise.
– StoryTeller
2 days ago
@StoryTeller: Since which flashy new standard?
– Bathsheba
2 days ago
1
@StoryTeller It was implementation-defined in C++03 too.
– Angew
2 days ago
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
2 days ago
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
2 days ago
|
show 3 more comments
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
.
3
implementation defined - I.e. look at the docs, and shout at your implementer if they don't do what they promise.
– StoryTeller
2 days ago
@StoryTeller: Since which flashy new standard?
– Bathsheba
2 days ago
1
@StoryTeller It was implementation-defined in C++03 too.
– Angew
2 days ago
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
2 days ago
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
2 days ago
|
show 3 more comments
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
.
-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
.
edited 2 days ago
Bakudan
13.3k84264
13.3k84264
answered 2 days ago
Bathsheba
172k27244366
172k27244366
3
implementation defined - I.e. look at the docs, and shout at your implementer if they don't do what they promise.
– StoryTeller
2 days ago
@StoryTeller: Since which flashy new standard?
– Bathsheba
2 days ago
1
@StoryTeller It was implementation-defined in C++03 too.
– Angew
2 days ago
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
2 days ago
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
2 days ago
|
show 3 more comments
3
implementation defined - I.e. look at the docs, and shout at your implementer if they don't do what they promise.
– StoryTeller
2 days ago
@StoryTeller: Since which flashy new standard?
– Bathsheba
2 days ago
1
@StoryTeller It was implementation-defined in C++03 too.
– Angew
2 days ago
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
2 days ago
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
2 days ago
3
3
implementation defined - I.e. look at the docs, and shout at your implementer if they don't do what they promise.
– StoryTeller
2 days ago
implementation defined - I.e. look at the docs, and shout at your implementer if they don't do what they promise.
– StoryTeller
2 days ago
@StoryTeller: Since which flashy new standard?
– Bathsheba
2 days ago
@StoryTeller: Since which flashy new standard?
– Bathsheba
2 days ago
1
1
@StoryTeller It was implementation-defined in C++03 too.
– Angew
2 days ago
@StoryTeller It was implementation-defined in C++03 too.
– Angew
2 days ago
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
2 days ago
@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
2 days ago
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
2 days ago
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
2 days ago
|
show 3 more comments
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%2f53388686%2funsigned-arithmetic-in-c%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
2
Why do you expect that an
int
would have the value 4294967286, which is much larger than the maximumint
?– molbdnilo
2 days ago
1
int b = -a;
->unsigned int b = -a;
– Paul R
2 days ago
1
@molbdnilo My fault. I forgot that
b
overflows, too.– Zhe Chen
2 days ago