Path: csiph.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.arch
Subject: Re: endian history
Date: Fri, 25 Sep 2026 03:19:50 -0700
Organization: A noiseless patient Spider
Lines: 60
Message-ID: <86zex5r5tl.fsf@linuxsc.com>
References: <10uks4f$1dqo$1@gal.iecc.com> <10vphh0$3o9jl$1@dont-email.me> <117gj6t$1b95v$2@dont-email.me> <6a9c1c93.39172234@news.eternal-september.org> <117j3es$24qgq$6@dont-email.me> <117n6ho$35j69$1@paganini.bofh.team> <6a9f7664.1891062@news.eternal-september.org> <2026Sep8.125659@mips.complang.tuwien.ac.at> <117pnpg$f179$2@dont-email.me> <11898n9$1nc06$2@dont-email.me> <118d6ld$1tkm9$1@paganini.bofh.team> <118dl5f$37cfv$1@dont-email.me> <118e8kg$3f05h$1@dont-email.me> <868q4zuhz4.fsf@linuxsc.com> <118jitv$1cm6b$1@dont-email.me> <86ik42tcf4.fsf@linuxsc.com> <118uf6m$12tta$1@dont-email.me> <86ld8srnq9.fsf@linuxsc.com> <1790188863-5857@newsgrouper.org> <86cxu3sp11.fsf@linuxsc.com> <1790269240-5857@newsgrouper.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Date: Fri, 25 Sep 2026 10:19:53 +0000 (UTC)
Injection-Info: dont-email.me; logging-data="3671296"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18UaXmiiRrm6981+CFdmpb1YCU7zG3Ag2A="; posting-host="f40942f53a0b7702eb43971178061210"
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:BKn25Ao+938gMQQnXVSRLh/UCdM= sha1:hQpxJLs1zu3sUKdTvABsCsNzQ6k= sha256:eU7mO1D88ZEeNXVaS5uFuADFBtJgguDYz+DtfVlklA0= sha1:CgpdRtUMXLAufbFjE6YpHx5fJH8= sha256:jZ7w/BYLvvQ6amK16hHIZlXRY4dj4txpZ5vOIyRgYqA=
Xref: csiph.com comp.arch:118203
MitchAlsup writes:
> Tim Rentsch posted:
>
>> MitchAlsup writes:
>>
>>> Tim Rentsch posted:
[considering a function to convert a signed argument, n, to
a balanced base 5 representation]
>>>> It looks like this statement cannot be right. In C, if n is
>>>> negative and not a multiple of 5, then n%5 is negative.
>>>
>>> An obvious case where n should be unsigned not signed.
>>
>> Yes and no. It is possible to solve this problem where the
>> divisions and remainders are always done on non-negative values
>> (and so could be unsigned rather than signed). I did in fact
>> code up a solution that has this property. But such code can be
>> harder to write and harder to understand. In contrast, it's
>> fairly easy to solve this problem using signed quantities and
>> simply deals with the negative remainders appropriately. Which
>> approach is better? As usual that can depend on other factors,
>> including performance.
First I misunderstood what you were suggesting; sorry about that.
The parameter n cannot be unsigned, because the purpose of the
function to produce a representation of a range of values, with
roughly half of them being negative.
> In my own code, 90%-odd of integer variables are unsigned. So,
> perhaps, over the decades, I have developed a way of coding that
> works better in unsigned space than in signed space. {{Almost
> every field in almost every CPU control register is inherently
> unsigned, as are the use of these as memory indexes, method-
> call indexes, and switch variables.}}
>
> I got to the point where if a negative value is not absolutely
> necessary, the variable is simply unsigned.
I get that. I'm pretty much the same in terms of preferring
unsigned types for integer values, and see similar ratios.
The point here is that this problem is inherently signed in at
least one aspect, because what is being converted is a signed
value. There is no point in using a balanced base encoding if all
the values being represented are non-negative.
Given that the input argument will be signed, the question is how
should the code be written to carry out the conversion? It can be
written using signed variables, like what Terje did. It also can be
written using unsigned variables, like the example function that I
posted. Which one is better? I wouldn't choose an unsigned-only
writing just because it doesn't used signed types (not counting the
signed argument). In most cases I give a higher weight to which
writing is easier to understand and easier to see how it works.
Other things being equal, I prefer using unsigned types. Here I am
not yet convinced that other factors are indeed equal.