The variable is unsigned so if you reach the limit, it goes on overflow and returns to the minimum value 0.
– SintuzNov 01 '20 at 08:20
7
`1024` has type `int` and won't be promoted to `long long` automatically. Use `1024ULL`.
– EvgNov 01 '20 at 08:21
@Evg Please turn that into an answer. It seems even already sufficiently explained.
– YunnoschNov 01 '20 at 08:22
@Sintuz `1024*1024*1024*1024` is `2^40` can be represented with 64 bits. No going over the top there. Even if it overflows and wraps around it shouldn't be 0.
– Tony TannousNov 01 '20 at 08:23
@Yunnosch, there should be a perfect dupe for this question. I'm looking for it now.
– EvgNov 01 '20 at 08:23
1
@Evg worth mentioning then int overflow is undefined behavior, any value could be expected than 0 as well?
– Tony TannousNov 01 '20 at 08:24
@Evg Fine with me. Either name it or provoke another user in providing it by making an answer. Please. I want this out of the list of unanswered questions.
– YunnoschNov 01 '20 at 08:24
@Evg I wish voting the quality of a found dupe were possible.... There are so many examples of abused hammers now. And of course I do not mean this one.
– YunnoschNov 01 '20 at 08:27
This is my first question in stack overflow. Thanks all , especially @Evg。Your answer is ok for me。@Evg
– JohnsonNov 01 '20 at 08:33
What you do with the result of an expression does not affect how the expression is evaluated. Your expectation that assigning it to a `long long` would cause the computation be done as a `long long` is not met.
– David SchwartzNov 01 '20 at 08:36