uint64_t t3 = MAXDWORD + 1 == 0?

  • A+
Category:Languages

I really don't understand what is happening with the code below. Why is t3 zero?

uint64_t t1 = MAXDWORD;         // t1 contains 4294967295 - CORRECT uint64_t t2 = t1 + 1;           // t2 contains 4294967296 - CORRECT uint64_t t3 = MAXDWORD + 1;     // t3 contains 0 (zero)   - HUH?? 

 


decltype(MAXDWORD) is a narrower type than uint64_t. So the expression MAXDWORD + 1 is not a uint64_t either, and unsigned wraparound behaviour is observed prior to the assignment to t3.

t1 + 1 is performed in unsigned arithmetic using the uint64_t type, which is wider.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: