Article View: comp.arch
Article #524Re: two's complement and Flags (Re: arithmetic and endian history)
From: David Brown
Date: Tue, 03 Nov 2026 07:35
Date: Tue, 03 Nov 2026 07:35
48 lines
3168 bytes
3168 bytes
On 03/11/2026 02:34, MitchAlsup wrote: > quadibloc@invalid.com (John Savard) posted: > >> On Mon, 02 Nov 2026 20:00:28 GMT, MitchAlsup >> <user5857@newsgrouper.org.invalid> wrote: >> >>> In what kind of arithmetic world does -i == i ??? >> >> Unless i=0, -i never equals i, that's true. >> >> On the other hand, if you use that letter in it's usual meaning as a >> constant, while i doesn't _equal_ -i, i is _isomorphic_ to -i... and >> to j, -j, k, and -k, if you want to include Hamilton's quaternions. > > I is just an integer. > > signed char i = 0xFF; It is better to write "int8_t i = 0xff;" here, since you are talking about a signed 8-bit integer, not whatever "char" might be on a particular system. And you mean "int8_t i = 0x80;" anyway. 0xff is -1 on an 8-bit two's complement machine - and then its negative is 1. > signed char j = -i; > > if( i == j ) // WTF > > NEGMAX == -NEGMAX in 2-s complement. > > I submit this is a lot stranger than having 2 zeros. > And that is why signed integer overflow is undefined behaviour in C. When you have overflows, there are no correct answers - no answers that make mathematical sense. Once you say that signed integer overflow is UB, and your code does not execute UB (or does not care what happens if it does), then signed integer types in C obey nice mathematical laws like "-i != i" except for when "i" is 0, and "a + b > a" if "b > 0". Two's complement wrapping is wonderful for tasks such as implementing multi-word arithmetic, and for efficient hardware implementations - but not at a higher level.
Technical Details
Message-ID:
<11cc2rq$3hhrv$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<10uks4f$1dqo$1@gal.iecc.com> <2026Oct3.191827@mips.complang.tuwien.ac.at> <1791071365-5857@newsgrouper.org> <11cai8f$32ue7$1@dont-email.me> <11caljv$jeo$1@gal.iecc.com> <1793649628-5857@newsgrouper.org> <6ae8f1c8.36096890@news.eternal-september.org> <1793669667-5857@newsgrouper.org>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: David Brown <david.brown@hesbynett.no> Newsgroups: comp.arch Subject: Re: two's complement and Flags (Re: arithmetic and endian history) Date: Tue, 3 Nov 2026 08:35:22 +0100 Organization: A noiseless patient Spider Lines: 48 Message-ID: <11cc2rq$3hhrv$1@dont-email.me> References: <10uks4f$1dqo$1@gal.iecc.com> <2026Oct3.191827@mips.complang.tuwien.ac.at> <1791071365-5857@newsgrouper.org> <11cai8f$32ue7$1@dont-email.me> <11caljv$jeo$1@gal.iecc.com> <1793649628-5857@newsgrouper.org> <6ae8f1c8.36096890@news.eternal-september.org> <1793669667-5857@newsgrouper.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 03 Nov 2026 07:35:23 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3721087"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Npusj2gfaGNsLbWxpPijJc3MgYpobyLM="; posting-host="f06b12b7403cf57442a23e3e44a23173" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:GERrSmCdt5RfiRJvVwyg4v4iBoE= sha256:uOJrWO2Ag+9KrgSDVxlHMLe4JxDf6dcfmqATE6IxYKE= sha1:saWHtzP8zriNYX+xCIlT6+zMp8c= sha256:QNcMdXcc7m/3BCkOKlU6SbGNhUKNd3V/LbN3ugLVpLk= In-Reply-To: <1793669667-5857@newsgrouper.org> Content-Language: en-GB