• Efficient dword bitwise rotate / circular shift using Bash shell ?

    From SugarBug@21:1/5 to All on Sat May 4 09:24:12 2024
    Example:

    rol() { num="$@" ; pre=$((num<<13)) ; suf=$((num>>19)) ;
    num=$((pre^suf)) ; num=$((num&0xffffffff)) ; echo "$num" ; }

    What tricks will make this more efficient in Bash?

    --
    www.sybershock.com | sci.crypt | alt.sources.crypto | alt.lite.bulb

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Phil Carmody@21:1/5 to SugarBug on Sat May 11 18:37:40 2024
    SugarBug <3883@sugar.bug> writes:
    Example:

    rol() { num="$@" ; pre=$((num<<13)) ; suf=$((num>>19)) ;
    num=$((pre^suf)) ; num=$((num&0xffffffff)) ; echo "$num" ; }

    What tricks will make this more efficient in Bash?

    If you can be sure the input will never be 0xffffffff, then this should
    work:

    rol () { echo "$(($1*8192%0xffffffff))" ; }

    Phil
    --
    We are no longer hunters and nomads. No longer awed and frightened, as we have gained some understanding of the world in which we live. As such, we can cast aside childish remnants from the dawn of our civilization.
    -- NotSanguine on SoylentNews, after Eugen Weber in /The Western Tradition/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jakob Bohm@21:1/5 to Phil Carmody on Wed May 15 13:33:01 2024
    On 2024-05-11 17:37, Phil Carmody wrote:
    SugarBug <3883@sugar.bug> writes:
    Example:

    rol() { num="$@" ; pre=$((num<<13)) ; suf=$((num>>19)) ;
    num=$((pre^suf)) ; num=$((num&0xffffffff)) ; echo "$num" ; }

    What tricks will make this more efficient in Bash?

    If you can be sure the input will never be 0xffffffff, then this should
    work:

    rol () { echo "$(($1*8192%0xffffffff))" ; }

    Phil


    rol13() { printf '0x%X' $(((($1<<13)+($1>>19))&0xffffffff))
    }

    Avoids that limitation, but may be slightly slower depending on bash inefficiencies.


    Enjoy

    Jakob
    --
    Jakob Bohm, CIO, Partner, WiseMo A/S. https://www.wisemo.com
    Transformervej 29, 2860 Søborg, Denmark. Direct +45 31 13 16 10
    This public discussion message is non-binding and may contain errors.
    WiseMo - Remote Service Management for PCs, Phones and Embedded

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