> Can I please have a language that actually has bitwise operators with defined semantics?
C++? It has defined semantics since C++20.
C++? It has defined semantics since C++20.
4) The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits
are filled with zeros. If E1 has an unsigned type, the value of the result
is E1 × 2**E2, wrapped around. If E1 has a signed type and nonnegative value,
and E1 × 2**E2 is representable in the result type, then that is the resulting
value; otherwise, the behavior is undefined.
5) The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an
unsigned type or if E1 has a signed type and a nonnegative value, the value of
the result is the integral part of the quotient of E1 / 2**E2. If E1 has a
signed type and a negative value, the resulting value is implementation-defined.
It's marginally more well-defined, sure, but not fully. The right shifts of negative numbers are no longer UB but are still an unportable mystery; and the left shifts of negative values are still outright UB.https://eel.is/c++draft/expr.shift
I thought this was part of the guaranteed two's complement change in C++20.
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/n49...
This is indeed from the C++20 paper P1236R1:
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p12...
In any case, thanks; it's nice to see C++ at least fully embracing the mandate on integers being two's complement, finally. There is enough footguns already without basic arithmetic operators being UB half of the time.