A mathematical question, thks! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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; }

3rd Sep 2018, 8:54 AM
Lawrence Lin
Lawrence Lin - avatar
8 Answers
+ 3
U means that it's an unsigned int, so no negative values! The max number on 32-bits is 4294967295.
3rd Sep 2018, 9:11 AM
Théophile
Théophile - avatar
+ 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.
3rd Sep 2018, 10:12 AM
Sergey Ushakov
Sergey Ushakov - avatar
3rd Sep 2018, 9:22 AM
KrOW
KrOW - avatar
+ 1
What is "u" in your code?
3rd Sep 2018, 9:04 AM
Théophile
Théophile - avatar
+ 1
i just type u, not other meanings.
3rd Sep 2018, 9:09 AM
Lawrence Lin
Lawrence Lin - avatar
+ 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.
3rd Sep 2018, 9:16 AM
Lawrence Lin
Lawrence Lin - avatar
+ 1
Maybe because a negative number is 42924967295 - number? I don't know, I'll do some search...
3rd Sep 2018, 9:19 AM
Théophile
Théophile - avatar
+ 1
Thanks everyone. I understand the reason. it just move diffence 3 from the largest number. thanks again !
3rd Sep 2018, 10:49 AM
Lawrence Lin
Lawrence Lin - avatar