r/C_Programming • u/am_Snowie • Oct 16 '26
Question is doing ~INT_MAX undefined behavior?
basically the title, i think producing INT_MIN by doing 1 << 31 is UB, so i thought of doing ~INT_MAX, but i am not sure if it's UB. sorry if it's dumb.
47
Upvotes
View all comments
Show parent comment
0
u/pskocik Oct 16 '26
Yeah. Doesn't quite work for signed right shifts. But for left shifts it does. Doing arithmetic shifts on signed right shifts is the sensible thing to do. I once played with detecting it with #if and doing some complicated workaround otherwise but it might make sense to just rely on the C++ behavior here and #error if it's not supported.