Shift right without Bitwise

  1. 2 weeks ago

    Garry P

    Apr 14 Pre-Release Testers, Xojo Pro Europe (Torquay, UK)

    I'm porting this C# code:

    i = 0x5f3759df - (i>> 1);

    It essentially shifts a 64-bit Double one place to the right. How do I achieve this without using the Bitwise module? It has to run on iOS. I know I've seen related code on the forum before but I can't find.

    Could someone who gets binary math help me out please?

    Thanks,

    Turns out if I finish my coffee - I can can figure it out myself!

    Public Function ShiftRight(i As Integer, places As Integer) as Integer
      Return i / (2 ^ places)
    End Function
  2. Garry P

    Apr 14 Pre-Release Testers, Xojo Pro Answer Europe (Torquay, UK)

    Turns out if I finish my coffee - I can can figure it out myself!

    Public Function ShiftRight(i As Integer, places As Integer) as Integer
      Return i / (2 ^ places)
    End Function
  3. Kem T

    Apr 14 Pre-Release Testers, Xojo Pro, XDC Speakers New York

    Use \ instead for integer division.

  4. Garry P

    Apr 14 Pre-Release Testers, Xojo Pro Europe (Torquay, UK)

    Will do. Out of curiosity, is that for performance reasons since both i and places are already Integers?

  5. Kem T

    Apr 14 Pre-Release Testers, Xojo Pro, XDC Speakers New York

    Yes. Without knowing for sure, I suspect / will transform both to doubles first, then back to integer whereas the compiler will convert n \ (2^places) to "shift right".

or Sign Up to reply!