If a standard document uses C code to describe std behavior, but that C code relies on UB, may an implementation do whatever they want?
-
-
If you actually want to express wrapping signed arithmetic (rare that it's wanted) there are easy, zero-cost ways to express it without UB.
-
you mean using unsigned and casts?... maybe. I was thinking for example signed ((x<<31)>>31) and similar, (x/(1<<31)) may use a divide, ...
-
partial nvm: '/' wouldn't work here, would need ex: ((x>=0)?0:-1) or similar, but similar issue. saner IMO to just make all this be defined.
-
Use unsigned types whenever you're doing bitwise arithmetic. Then it's just -(x>>31).
-
That simplifies to a single SAR instruction on any decent compiler architecture.
-
but signed >> works on most targets and compilers I have used (excepting for MSP430 and AVR and similar), and a lot of code relies on it.
-
>> on signed is implementation-defined, not undefined, and fairly safe to rely upon. << on negative is undefined. Overflow is always undef.
-
When x is signed, x<<n is equivalent to x*(1<<n) except the former is defined on a smaller domain; just use the latter if you care.
- 8 more replies
New conversation -
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.