the problem is that from what I understood, the excepted values in this loop were -1 to 9, but somehow, it was still running fine and working as intended ??
probably works like UINT_MAX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9? idk why would anyone need max value there, but I guess they knew what they were doing if that runs fine
Yep. Unsigned overflow is well defined, and it will always just wrap. I'd assume the other end of that says something like ; dx++), so it will just go 0, 1, 2, etc.
Depends on the language - and CPU architecture. It's well defined on basically all modern architectures, but Rust treats it as an undefined operation. Rust would also emit a compiler error for attempting to assign a negative value to an unsigned variable.
So it's a language specific feature, your comment is pointless except doing rust propaganda.
For instance in c, before two's complement were added as a standard, the integer representation choice was left to the implementation. So integer overflfow was undefined behavior.
negative numbers are a lie and the only math operations that actually distinguish between them on modern hardware is comparison (>, >=, <, <= specifically, == and != don't care either), multiplication and division, signed and unsigned addition and subtraction both function identically in hardware and use the same assembly instruction
They all do, actually. Your hardware doesn't cares how you compute a number, just how you interpret it. The hardware just moves bits around
Case in point: comparisons. On most ISAs they use just one instruction (although many allow you to fuse it with arithmetic), which is almost always mapped to subtraction. Update bitflags based on the result (zero, over/underflow etc) and do what you want. Comparisons are also stateless
I see comparison as a combination of the cmp and of the actual storing a boolean, but yea, it's usually just a cmp, multiplication and division do have distinct signed and unsigned versions tho, no?
Compare doesn't really "returns" anything on the common sense of returning. Most ISAs have a dedicated FLAGS register (RFLAGS on x86-64 or NZCV on aarch64) that stores the "results" of arithmetic operations in regards to sign*. A SUB/Jcc is functionally identical to a CMP/Jcc, only that SUB destroys the values on the registers while CMP doesn't
multiplication and division do have distinct signed and unsigned versions tho, no?
Well...yes and no. Yes, there does exists different instructions for signed/unsigned multiplication, but signed is almost never used. The reason is historical: multiplication has always been tricky because given a number with N bits, N * N does not necessarily fit within these N bits
As a result, multiplication has historically always been a 2N operation, and that's where MUL (signed multiplication) comes in. MUL only takes one single register and returns the result on RDX:RAX (or some other combination, i don't remember lol), which is effectively a 128 bits return value
This was important during 16b (and rarely 32b) eras where the limits weren't that huge. Its super easy to get a 32b result with 16b multiplication. Its nowhere as easy to get a 128b result with 64b multiplication, so you don't need a 128b return. Because of that, IMUL (unsigned multiplication, and IMUL alone) takes two values. MUL does not. As a result, compilers only ever emit MUL** if they need the upper bits of the 128b integer, which is rare
As for division, yes, it's separate. And compilers do differentiate. But DIV/IDIV are two of the slowest instructions there is, so they get aggressively optimized out. Floating point math is miles faster, but you can't have an unsigned float :p
*: this again depends on ISA. On x86 all arithmetic operations update flags, so you'll usually see comparisons right before jumps. On aarch64 this isn't the case, each instruction is encoded with a bitmask that describes which flag that instruction can update
**: aarch64 doesn't have this issue. It still emits unsigned/signed instructions when it needs 128b, but the actual multiplication instruction is actually just a hardware macro for the repeated addition instruction. So no sign either!
compare as an instruction does not, but if I have a function like (a, b) => a < b it needs to move a boolean into something, the operator < encodes both the compare instruction (which "returns" flags, I am aware), and the conditional moves / branching needed to store a true / false boolean into the return value, which does need to know if we're doing a signed or unsigned comparison
But that's exactly my point, the sign is relevant when interpreting the data or the results, not when calculating. The same flags will be updated regardless of the sign, you just choose to use it or not
when I say it matters for comparisons, I am including the part where the comparison gets used for branching / stored into a variable, u8+u8 and i8+i8 generate identical assembly, u8>u8 and i8>i8 do not
**: aarch64 doesn't have this issue. It still emits unsigned/signed instructions when it needs 128b, but the actual multiplication instruction is actually just a hardware macro for the repeated addition instruction.
Not sure what you mean by "hardware macro". The hardware itself will use parallel branching trees to perform integer multiplication but that's still a specific instruction. Do you mean MUL Xd, Xn, Xm === MADD Xd, Xn, Xm, XZR where XZR is the 0 register?
Also I think you swapped MUL with IMUL. Compilers prefer the signed version.
Compilers prefer the MUL instruction on your example because you disabled optimizations. Of course it won't optimize anything lol. https://godbolt.org/z/x468TTn7f opt level 3 will produce an IMUL, as expected. MUL is almost never emitted on performant code because it destroys registers, which are already quite tight on x86-64
This is a common idiom for setting large unsigned numbers in C (honestly, usually just all 1s set/MAX_INT, though you could do the same conceptual trick with -2, etc).
Wraparound for any unsigned integer type in C is defined modulo MAX_VAL + 1. I'll specifically demonstrate the case of unsigned int (and I'll assume 32-bit integers):
This is really interesting, I always thought integer overflow was undefined behavior, but I just checked and apparently that is only the case for signed integers, for unsigned you're right, this is totally fine. TIL.
The conversion from signed to unsigned integer (which is what is happening here) is defined in exactly the same way in C, regardless of whether we're working with 1s complement, 2's complement, or sign-and-magnitude.
Converting a negative number to an unsigned integer happens conceptually by adding 2^N (where N is the bitwidth) — i.e. UNIT_MAX +1 — to get a positive number.
Again, this happens regardless of the underlying representation chosen for signed integers
In terms of what it means in two's complement, it is a no-op, but until C23 it's always deliberately been defined in arithmetic terms, instead of bit-representation terms.
In fact, I'm pretty positive it's still defined in arithmetic terms in the C23 standard, even though it doesn't necessarily need to be
398
u/_XYZT_ May 05 '26
UINT_MAX
82
u/ironykarl May 06 '26 edited May 06 '26
This is a common idiom for setting large unsigned numbers in C (honestly, usually just all 1s set/MAX_INT, though you could do the same conceptual trick with
-2, etc).Wraparound for any unsigned integer type in C is defined
modulo MAX_VAL + 1. I'll specifically demonstrate the case ofunsigned int(and I'll assume 32-bit integers):69
u/Heniadyoin1 May 06 '26
That's a normal pattern?
It's a common flag value, even for unsigned numbers
9
u/un_virus_SDF May 06 '26
If it's to rely on overflfow i'd rather do
UINT_MAXor~(uint)020
u/zoner197 May 05 '26
Top 10 programmers of 2026
2
u/asmanel May 06 '26
Maybe an attempt to determine the size of a variable of this type by someone who don't know sizeof. However, I'm not sure the compiler will agree.
2
u/Interesting_Buy_3969 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” May 06 '26
On most platforms it'll just set all bits of the register value to 1.
2
u/steadyfan May 06 '26
Short hand for 0xFFFF
2
u/CharlemagneAdelaar May 07 '26
That’s groty cuz it relies on like ppl who don’t know this pattern to think for a few extra secondsvs using UINT_MAX which is self-documenting
2
u/facebrocolis May 07 '26
Someone forgot either a differential on the right or an integral on the left side
1
u/Beetmaker69 Oct 20 '26
And it just works. You cannot change it