logoalt Hacker News

Joker_vD • today at 3:58 AM • 2 replies • view on HN

> Shifting the sign bit out is UB

Shifting into the sign bit is also UB. Can I please have a language that actually has bitwise operators with defined semantics? No CPUs had differentiated between SAL/SHL for more than 30 years, if that was the original justification.

I'm fine with left shift trapping on overflow. I'm fine with it just wrapping around. I'm even fine with bitwise shifts simply unapplicable to ints ("cast to unsigned first, then cast back to int"). I am not fine with an operator that invokes UB for literally half of the values of the lhs's domains.


Replies

uecker • today at 5:28 AM

I was thinking about eliminating this shift-into-left bit UB. There was a previous attempt though that failed to get consensus. (but long enough ago that one could try again).

But all this signed overflow UB can be turned into traps using sanitizers. In practice, generally mandating a trap is difficult though in ISO C. My plan is to mandate this as part of a special mode, in which case I would just leave this UB in the main standard.

(One problem we also have that some special interests groups in WG14 are rather hostile to our work and the rest of the C community - so I encourage everybody who is actually using C and caring about it to join).

➕ show 1 reply
dcrazy • today at 4:34 AM

Swift: https://developer.apple.com/documentation/swift/binaryintege...

> The << operator performs a smart shift, which defines a result for a shift of any value.

> - Using a negative value for rhs performs a right shift using abs(rhs).

> - Using a value for rhs that is greater than or equal to the bit width of lhs is an overshift, resulting in zero.

> - Using any other value for rhs performs a left shift on lhs by that amount.

And while the docs don’t say it, `Int.max << 1 == -2`. And in fact, `T.max << 1 == -2` for all `T: BinaryInteger`.