3 ms·
I don't know about other bigint libraries, but java.math.BigInteger uses two's complement, not sign-magnitude. This class is designed to imitate machine-size tw
by nayuki 3y ago
I don't know about other bigint libraries, but java.math.BigInteger uses two's complement, not sign-magnitude. This class is designed to imitate machine-size two's complement integers.
The most negative value is a wart in two's complement, but negative zero is a wart in sign-magnitude and ones' complement.
The advantage of ones' complement compared to sign-magnitude is that the former needs minimal circuitry to implement subtraction. Namely, it just needs to invert every bit and then feed it into the adder.
You only need to avoid undefined behavior in two's complement in C and C++. UB is not a property of two's complement, but a particular language's design choices. I can assure you that there is no UB in two's complement in Java or x86 assembly.
Note that offset-binary is used in IEEE 754 floating-point exponents.
- account42 3y ago> You only need to avoid undefined behavior in two's complement in C and C++. UB is not a property of two's complement, but a particular language's design choices. I can assure you that there is no UB in two's complement in Java or x86 assembly. The entire point of making unsigned arithmetic UB in C was to support different bit representations of negative numbers depending on the HW. I think the GP's point was that if you want two's complement wraparound behavior in languages where you are not allowed to wrap around signed integers you can get the same result with unsigned arithmetic.
- o11c 3y agoThat's wrong, actually. It uses sign-magnitude internally, it just supports various bit-related operations as-if it were two's complement. https://github.com/openjdk/jdk/blob/28026434f74726d72857e781b8f1c3d12aba9125/src/java.base/share/classes/java/math/BigInteger.java#L173 https://github.com/openjdk/jdk/blob/28026434f74726d72857e781...