Circular shift

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1998
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Circular shift

Post by [-hh] » 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.
shiftLock happens

Ledigimate
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 132
Joined: Mon Jan 14, 2013 3:37 pm

Re: Circular shift

Post by Ledigimate » Mon Dec 17, 2018 8:07 pm

[-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).
010100000110010101100001011000110110010100111101010011000110111101110110011001010010101101010100011100100111010101110100011010000010101101001010011101010111001101110100011010010110001101100101

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1998
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Circular shift

Post by [-hh] » Mon Dec 17, 2018 8:43 pm

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 ...
shiftLock happens

Ledigimate
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 132
Joined: Mon Jan 14, 2013 3:37 pm

Re: Circular shift

Post by Ledigimate » Tue Dec 18, 2018 12:30 am

[-hh] wrote:
Mon Dec 17, 2018 8:43 pm
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 ...
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
010100000110010101100001011000110110010100111101010011000110111101110110011001010010101101010100011100100111010101110100011010000010101101001010011101010111001101110100011010010110001101100101

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1998
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Circular shift

Post by [-hh] » 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).
shiftLock happens

Ledigimate
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 132
Joined: Mon Jan 14, 2013 3:37 pm

Re: Circular shift

Post by Ledigimate » Thu Dec 20, 2018 3:48 pm

[-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.
010100000110010101100001011000110110010100111101010011000110111101110110011001010010101101010100011100100111010101110100011010000010101101001010011101010111001101110100011010010110001101100101

Post Reply

Return to “Talking LiveCode”