+ 1

unsigned modifier holds only positive value. Then why it gives something random positive integer on assigning -ve value?

unsigned int a = -1; gives 4294967295 which is max value of unsigned int and for -2 4294967294 basically starting reverse from max on -ve values. Explain?

28th Apr 2021, 10:59 AM
Shunon
Shunon - avatar
4 Respostas
+ 2
Why so? I believe it's due to the effect of integer overflow 👇 https://en.m.wikipedia.org/wiki/Integer_overflow
28th Apr 2021, 11:58 AM
Ipang
+ 2
`unsigned int` is 32bit integer with 0 as min value, and (2 ^ 32) - 1 as max value. 2 ^ 32 => 4.294.967.296 When you try to assign a negative value for an `unsigned int` variable, add the max value above by the negative value. 4.294.967.296 + (-1) => 4.294.967.295 4.294.967.296 + (-2) => 4.294.967.294 And so on ...
28th Apr 2021, 11:40 AM
Ipang
+ 1
Ipang but why so? ;-;
28th Apr 2021, 11:52 AM
Shunon
Shunon - avatar
+ 1
Yeah get it now thank you Ipang
28th Apr 2021, 12:04 PM
Shunon
Shunon - avatar