Page 2 of 2
Re: Circular shift
Posted: Mon Dec 17, 2018 7:47 pm
by [-hh]
Gerrie.
You could also try to use leftshift and rightshift of the shell, for example
Code: Select all
put shell("echo $((2 << 4)); echo $((1024 >> 3))")
There piping is also available.
Re: Circular shift
Posted: Mon Dec 17, 2018 8:07 pm
by Ledigimate
[-hh] wrote: Mon Dec 17, 2018 7:47 pm
Gerrie.
You could also try to use leftshift and rightshift of the shell, for example
Code: Select all
put shell("echo $((2 << 4)); echo $((1024 >> 3))")
There piping is also available.
It gives me "<< was unexpected at this time." on my system (Win 10 Pro).
Re: Circular shift
Posted: Mon Dec 17, 2018 8:43 pm
by [-hh]
Oh, sorry. No default bash or ksh on Windows.
But did you already try do as "vbscript"?
Code: Select all
if the platform is "Win32" then
do "result = 2 << 4" as "vbscript"
put the result
else
...
end if
See
https://docs.microsoft.com/en-us/dotnet ... t-operator
and
https://stackoverflow.com/questions/331 ... sual-basic
Just to collect all options ...
Re: Circular shift
Posted: Tue Dec 18, 2018 12:30 am
by Ledigimate
Hermann, you are my hero. The "do
statementList as
alternateLanguage" method is just
SO much easier than using a browser widget! Why haven't we thought of that sooner?
Just one minor adjustment: although recent versions of Visual Basic do have the << or >> operators, VBScript does not. But JavaScript does, as shown earlier. So now we can do this:
Code: Select all
function rotateBits pN, pP, pD -- 32-bit numbers only
if pD is "L" then
do "result = (" & value(pN) & " << " & value(pP) & ") | (" & value(pN) & " >>> (32 - " & value(pP) & "))" as "JavaScript"
else
do "result = (" & value(pN) & " >>> " & value(pP) & ") | (" & value(pN) & " << (32 - " & value(pP) & "))" as "JavaScript"
end if; return the result
end rotateBits
What a journey it was to bring us to this little 5-line function. Thank you all!
Gerrie
Re: Circular shift
Posted: Tue Dec 18, 2018 12:48 am
by [-hh]
Sadly do as "javascript" is currently available ONLY for HTML5-standalones (Emscripten).
For desktop/mobile we have to use the javascript of libbrowser (accessible via a browser widget).
And the browser widget doesn't currently work on most linux flavours.
So you have to do a platform switch or use your pure LC versions (which works everywhere).
Re: Circular shift
Posted: Thu Dec 20, 2018 3:48 pm
by Ledigimate
[-hh] wrote: Tue Dec 18, 2018 12:48 am
Sadly do as "javascript" is currently available ONLY for HTML5-standalones (Emscripten).
For desktop/mobile we have to use the javascript of libbrowser (accessible via a browser widget).
And the browser widget doesn't currently work on most linux flavours.
So you have to do a platform switch or use your pure LC versions (which works everywhere).
But you're still my hero.
You mentioned in an earlier post (
https://forums.livecode.com/viewtopic.p ... 12#p174312) that one could use *2^pPosition and div 2^pPosition. Well, it turns out you were correct.
So here's a pure LC 5-line function:
Code: Select all
function rotateBits pNumber, pPositions, pDirection -- 32-bit numbers only
if pDirection is "right" then
return (pNumber div (2^pPositions)) bitOr ((pNumber * (2^(32 - pPositions))) bitAnd 4294967295)
else
return ((pNumber * (2^pPositions)) bitAnd 4294967295) bitOr (pNumber div (2^(32 - pPositions)))
end if
end rotateBits
Thank you, Hermann.