Bitwise Operations

Home Forums General Programming Bitwise Operations

Tagged: 

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #54739
    Puppeteer
    Participant
      • Topics: 16
      • Replies: 185
      • Total: 201
      • ★★

      Hi guys,

      I’m just in the process of doing bit packing and unpacking functions for the Kurzweil PC3 (unfortunately it’s different to the Morpo method that’s already build into Ctrlr).

      Just putting a list of bitwise functions for LUA that I found to remind myself, and also in case anyone else is looking for them in the future.

      This appears to be the list of available bitwise operations:
      bit.tobit(x) — normalize number to the numeric range of
      bit.tohex(x[,n]) — convert x to hex with n digits (default 8)
      bit.bnot(x) — bitwise not of x
      bit.band(x1[,x2…]) — bitwise and of x1, x2, …
      bit.bor(x1[,x2…]) — bitwise or of x1, x2, …
      bit.bxor(x1[,x2…]) — bitwise xor of x1, x2, …
      bit.lshift(x, n) — left-shift of x by n bits
      bit.rshift(x, n) — logical right-shift of x by n bits
      bit.arshift(x, n) — arithmetic right-shift of x by n bits
      bit.rol(x, n) — left-rotate of x by n bits
      bit.ror(x, n) — right-rotate of x by n bits
      bit.bswap(x) — byte-swap of x (little-endian <-> big-endian)

      From http://www.leadwerks.com/werkspace/topic/11020-bit-shifting-in-lua/

      Also, these pages may be helpful – http://www.lua.org/manual/5.3/manual.html
      Section 6.7 of http://www.lua.org/manual/5.2/manual.html#6.7

      Still testing but I think both bit.band and bit32.band will work equally. Happy to be corrected if that’s incorrect.

      The Puppeteer
      http://godlike.com.au

      #58045
      bloodykot
      Participant
        • Topics: 0
        • Replies: 7
        • Total: 7

        Thanks! Is it possible to use LUA’s bitwise operators (&,||,~,>>,<< etc.) instead of bitwise functions (bit.band, bit.bor etc.)?

        #58063
        Puppeteer
        Participant
          • Topics: 16
          • Replies: 185
          • Total: 201
          • ★★

          From my experiments, these don’t work. I think that the lua used in ctrlr is not the latest version where the operators were introduced.

          The Puppeteer
          http://godlike.com.au

          #58064
          atom
          Keymaster
            • Topics: 159
            • Replies: 2945
            • Total: 3104
            • ★★★★★

            You can’t use those. I use lua 5.1 i can’t upgrade to 5.2 without some issues, so for now there are not built-in bit operations, only those available in the bit. module.

          Viewing 4 posts - 1 through 4 (of 4 total)
          • The forum ‘Programming’ is closed to new topics and replies.
          There is currently 0 users and 92 guests online
          No users are currently active
          Forum Statistics
          Threads: 2,495, Posts: 17,374, Members: 77,605
          Most users ever online was 12 on January 22, 2019 3:47 pm
          Ctrlr