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
2
u/TheThiefMaster Oct 16 '26
Unsigned right shift (logical shift) behaves differently to signed right shift (99.9% arithmetic right shift) - that's not a workaround if that's what you want.
Plus - I'm pretty sure casting to/from unsigned is also implementation defined for negative numbers