logoalt Hacker News

SAI_Peregrinus • today at 3:54 AM • 1 reply • view on HN

I think it means what you think is ridiculous. Shifting the sign bit out is UB. Unrepresentable results are UB.


Replies

Joker_vD • today at 3:58 AM

> 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.

➕ show 2 replies