in reply to Re: Detecting whether UV fits into an NV
in thread Detecting whether UV fits into an NV
There are a few problems with this code, mostly revealed by running clang's address sanitizer on it.
One is that the taking the negative of the most negative number expressible on a 2's complement box yields undefined behavior. The solution is to instead always do -(t + 1) + 1U That works for all negative values
Second is that the code assumes that this is a 2's complement machine. You rarely find a 1's complement these days, so this isn't a major issue. But it is trivial to avoid it. To get the lsb use t & (~t + 1);
Finally, it is undefined behavior to left shift a negative number, which it could be since you've declared last_set to not be unsigned, and the top bit could be 1
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Detecting whether UV fits into an NV
by syphilis (Archbishop) on Aug 18, 2025 at 09:04 UTC |