Results 1 to 10 of 10

Thread: SHR and SHL?

  1. #1
    Join Date
    Nov 2007
    Location
    Nowhereville
    Posts
    1,155
    Mentioned
    0 Post(s)
    Quoted
    3 Post(s)

    Default SHR and SHL?

    Hi everyone,

    I was looking at the SRL include and I noticed these:

    SCAR Code:
    shl 10
    shr 10

    All i could dig up is that they have something to do with binary and that they mean Shift Left and Shift Right. What do they actually do, and how are they useful in autoing?

    Thanks for your help
    Formerly known as Cut em2 it

  2. #2
    Join Date
    Oct 2006
    Posts
    468
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    This is nearly a complete guess, but I think that the 10s are the keycodes for shift.

  3. #3
    Join Date
    Jun 2006
    Posts
    3,861
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

    It moves all of the bits left (shl) or right (shr) a certain number of places. For example if your integer is, in binary, 11001100, and you shr 2'd it, it would become 00110011. If you shl 2'd it, it would become 00110000. Does that make sense?

  4. #4
    Join Date
    Nov 2007
    Location
    Nowhereville
    Posts
    1,155
    Mentioned
    0 Post(s)
    Quoted
    3 Post(s)

    Default

    Okay this makes sense, but how does this improve our scripting?
    Formerly known as Cut em2 it

  5. #5
    Join Date
    Jan 2010
    Location
    UK
    Posts
    129
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    I believe shifting left is the same as multiplying by 2, so shift n is multiply by 2^n. This is quicker (executed in fewer CPU cycles) than straight multiplication. That's the only use I can think of (it's also useful for storing and extracting single bits of information if your tight on memory...), although pointless as it would make virtually no difference in the context of SCAR.

    I'd only investigate it purely for academic reasons :P

  6. #6
    Join Date
    Oct 2006
    Location
    ithurtsithurtsithurtsithurts
    Posts
    2,930
    Mentioned
    7 Post(s)
    Quoted
    135 Post(s)

    Default

    Quote Originally Posted by silentwolf View Post
    I believe shifting left is the same as multiplying by 2, so shift n is multiply by 2^n. This is quicker (executed in fewer CPU cycles) than straight multiplication. That's the only use I can think of (it's also useful for storing and extracting single bits of information if your tight on memory...), although pointless as it would make virtually no difference in the context of SCAR.

    I'd only investigate it purely for academic reasons :P
    Speed is the exact reason it's used in the include, although only certain scripters really use it (Wizzup? is the only one that springs to mind).

  7. #7
    Join Date
    Nov 2007
    Location
    Nowhereville
    Posts
    1,155
    Mentioned
    0 Post(s)
    Quoted
    3 Post(s)

    Default

    o ok, thanks guys!
    Formerly known as Cut em2 it

  8. #8
    Join Date
    Oct 2009
    Location
    Stockton, CA
    Posts
    2,040
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    They are bitwise operations. Shr and shl shift left and right appropriately.
    They are pretty much used for super fast math.
    Join the IRC! irc.rizon.net:6667/srl | SQLite (0.99rc3+) | SRL Doc | Simba Doc | Extra Simba Libraries (openSSL & sqlite3)
    Quote Originally Posted by #srl
    10:45 < Toter> daphil when can get sex anyday I want
    10:45 < Toter> he is always on #SRL
    "A programmer is just a tool which converts caffeine into code"

  9. #9
    Join Date
    Sep 2006
    Posts
    5,219
    Mentioned
    4 Post(s)
    Quoted
    1 Post(s)

    Default

    The speed difference in SCAR is debatable, but it's good practice if you want to use other languages.

  10. #10
    Join Date
    Feb 2006
    Location
    Amsterdam
    Posts
    13,692
    Mentioned
    146 Post(s)
    Quoted
    130 Post(s)

    Default

    Quote Originally Posted by senrath View Post
    Speed is the exact reason it's used in the include, although only certain scripters really use it (Wizzup? is the only one that springs to mind).
    Well, whenever I use that it usually isn't for speed. When I first used shr and shl, I was just experimenting with it a bit. It's fun to use, and it actually taught me bitwise pretty well. The speed difference is minimal in SCAR. (But then again, it is interpreted anyway...)



    The best way to contact me is by email, which you can find on my website: http://wizzup.org
    I also get email notifications of private messages, though.

    Simba (on Twitter | Group on Villavu | Website | Stable/Unstable releases
    Documentation | Source | Simba Bug Tracker on Github and Villavu )


    My (Blog | Website)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •