Hacker News new | ask | show | jobs
by IncRnd 1326 days ago
There are still edge cases here - various posters here have mentioned them.

The proper method is to type promote first - not just to unsigned but to a wider variable type - 32 to 64 bits or from 64 to 128 bits. Unsigned simply gives a single extra bit, while erasing negative semantics. Promoting to twice the size works for either addition or multiplication. The benefits are correctness and the ability to be understood at a glance.

1 comments

> There are still edge cases here - various posters here have mentioned them.

Are you sure? What's an example of an array.length that would trigger a remaining edge case here? (Keep in mind array.length is 32-bit in Java.)