• Arithmetic trivia (Was: [ANN] KornShell 93u+m 1.0.0-beta.2)

    From Kenny McCormack@21:1/5 to spibou@gmail.com on Fri Dec 17 13:10:37 2021
    In article <JGDDG6C28aBHRfrQ6@bongo-ra.co>,
    Spiros Bousbouras <spibou@gmail.com> wrote:
    On Fri, 17 Dec 2021 11:18:26 +0100
    Martijn Dekker <martijn@inlv.demon.nl> wrote:
    What the announcement item above means instead is this:

    $ myfunc() { return 12345; }
    $ myfunc; echo $?
    12345
    $ (myfunc); echo $?
    57
    $ ksh -c 'myfunc() { return 12345; }; myfunc'; echo $?
    57

    i.e., the exit status of a subshell or child shell that invokes
    'myfunc', and does nothing to overwrite $? before exiting, is the least
    significant 8 bits of 12345, i.e., 12345 % 0xFF == 210.

    For the least significant 8 bits of 12345 the calculation you want is
    12345 % 256 == 57 .Regardless , 12345 % 0xFF == 105 .

    If you want to use 255 (i.e., hex FF), then you have to use an 'and'
    operator.

    i.e., either of:

    12345 % 256

    or:

    12345 & 255

    --
    If the automobile had followed the same development cycle as the
    computer, a Rolls-Royce today would cost $100, get a million miles to
    the gallon, and explode once every few weeks, killing everyone inside.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Kenny McCormack on Fri Dec 17 21:39:09 2021
    On 17.12.2021 14:10, Kenny McCormack wrote:

    If you want to use 255 (i.e., hex FF), then you have to use an 'and' operator.
    i.e., either of:
    12345 % 256
    or:
    12345 & 255

    Yep, just a typical well known careless mistake. In the same vein
    as implementing the integer division
    x div 2 # x/2
    by
    x shr 1 # x>>1
    but writing by accident x>>2 (a careless mistake I made 35 years
    ago in a test and still recall); a favorable reader will understand
    the intention, but a computer will merciless execute it.

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)