Discussion:
Efficient dword bitwise rotate / circular shift using Bash shell ?
(too old to reply)
SugarBug
2024-05-04 14:24:12 UTC
Permalink
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
Phil Carmody
2024-05-11 15:37:40 UTC
Permalink
Post by SugarBug
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/
Jakob Bohm
2024-05-15 11:33:01 UTC
Permalink
Post by Phil Carmody
Post by SugarBug
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
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
Loading...