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
19
u/AciusPrime Oct 16 '26
The standard way of producing INT_MIN is to write “INT_MIN.” If you want to see how your standard library produces it, you can go rooting around in limits.h and find where they declared it. It’s annoying to reliably produce it correctly without already knowing what it is, which is why it’s available in the library.
The “31” implies you already know and assume your program is running on a machine where int is 32-bits.
65
u/olorochi Oct 16 '26 edited Oct 16 '26
C23 guarantees that integers are represented using two's complement so you can safely assume ~INT_MAX == INT_MIN there. Earlier versions allowed any representation for integers so relying on that assumption would be unsafe. For example with a sign-magnitude system you would get -0 instead.
Edit: correct one's complement to sign-magnitude
4
u/Duck_Devs Oct 16 '26 edited Oct 16 '26
Shouldn’t be. 1 << 31 is UB because not every platform has 32 bit int. (edit: this doesn’t mean I think this isn’t UB on platforms on which it works.)
Doing a simple bitwise NOT on a number, though, is fine since you let the hardware decide how many bits to flip. Also, since C23, all signed ints are two’s complement so results should be consistent.
1
u/GenericFoodService Oct 16 '26
In older versions of C, it technically is undefined behavior, but if you're on any modern system the behavior defined by the hardware is consistent with what you're expecting. You can also independently verify what the behavior is with simple tests like this, and if you want to know how it behaves on various different systems, you can use things like https://godbolt.org to test snippets and tell it what architecture it should output. This code produces the same output for x86, and ARM
2
u/sciencekm Oct 16 '26
It looks like you just want to define your own INT_MIN.
The following should work for 2s complement (which is practically everything these days).
#define MY_INT_MIN ((int)(((~0u)>>1)+1))2
u/DawnOnTheEdge Oct 17 '26 edited Oct 17 '26
C23 requires that signed integral types use two’s-complement representation. This means that
-INT_MAXis legal (and equal toINT_MIN + 1) but-INT_MINcauses signed integer overflow, which is undefined behavior.Previous versions of the standard technically allowed implementations to use sign-and-magnitude representation or one’s-complement representation instead (in which case
-INT_MAX == INT_MIN). This was there for backward compatibility with some mainframes from the 1960s.2
u/TheThiefMaster Oct 16 '26 edited Oct 16 '26
C++ made this explicitly legal, even before it standardised on 2s complement, by defining the behaviour as the same as an unsigned->signed conversion as long as the shift would have fit in the unsigned version of the type (which IIRC is an implementation-defined conversion, rather than undefined, before C++20 forced 2s complement and made it defined).
By my reading it's UB in older C and is still UB in C23, however doing
(signed)(1u << 31)instead might actually be legal?(assuming int is 32-bit, of course)