+ 2
A mathematical question, thks!
I am a new learner for c++. Why do I type “-4u”, I get 4294967292 ? Thank you for your help. #include <iostream> using namespace std; int main() { cout << -4u; return 0; }
8 ответов
+ 3
U means that it's an unsigned int, so no negative values!
The max number on 32-bits is 4294967295.
+ 3
Lawrence Lin Think about numbers as it is a ring. The lowest positive is 0. The value before 0 is a highest one which is 4294967295. So, -1 == 0-1 == 4294967295.
The reason of this behavior is the method of storing negative values. There's no real difference between signed and unsigned values in the computer memory. The difference is in our interpretation of them. When you say, that highest bit does not stores sign by adding u to the number you specify the rules of reading, comparison, and other, but not the number itself. If you read 4294967292 as signed value you will receive -4.
+ 2
+ 1
What is "u" in your code?
+ 1
i just type u, not other meanings.
+ 1
i know it. The max number on 32-bits is 4294967295. but why -4u is 4294967292 ? i am confuse that i just add -4 to u and the diffence is 3.
+ 1
Maybe because a negative number is 42924967295 - number?
I don't know, I'll do some search...
+ 1
Thanks everyone. I understand the reason. it just move diffence 3 from the largest number. thanks again !