6

Currently I am trying to decode data (using lua). Data I have is 64 bit hexadecimal (big endian).

That specific number needs to be converted to little endian. Beside that other bitwise operations are going to be applied later based on the internal logic.

Now, initially I thought that things are going to be easy, having in mind that I can use bitop and simply apply any bitwise operations I would like to.

Now, unfortunately, I have (and please make a note that I am not able to change this), 32 bit lua compiler, hence, I am not simply able to use these operations.

There are 2 questions which I would like to ask:

  • If i have hex

    800A000000000000 how can I use 32 bitwise operations (specifically in this case operations like bit.bswap, bit.rshift, bit.lshift from the library i shared above) on this hexadecimal number? Is it possible to split this number on 2 parts, and then apply operations on each of them and then merge them together again? If that is the case, how?

  • Do you have any good reference (like a good book) which I can use to familiarize myself with algorithms and what I can do and what I cannot when we are talking about bitwise operations (expecially representing huge numbers using multiple small chunks )

Prior asking this question, I found and read other references to the same subject, but all of them are more related with specific cases (which is hard to understand unless you are familiar with the subject):

  1. lua 64-bit transitioning issue
  2. Does Lua make use of 64-bit integers?
  3. Determine whether Lua compiler runs 32 or 64 bit
  4. Comparing signed 64 bit number using 32 bit bitwise operations in Lua
cool
  • 3,225
  • 3
  • 33
  • 58
  • Which version of Lua are you using? Does it have integers (5.3) or just floats (5.2 and earlier)? – cyclaminist Apr 18 '18 at 22:57
  • 5.2 version of lua. We are planing to change things and use 64bit version as well. But after yesterday research i figure out that does not means a lot to be honest having in mind that number above is bigger then 2^52 which brings me to the same issue as I had initially. Seems that all of the bitops in lua applied on numbers above 2^52 resulting with 0. – cool Apr 19 '18 at 01:25
  • 1
    Perhaps that's because the largest integer that can be represented with a precision of 1 in a 64-bit float is 2^53 (see [this answer](https://stackoverflow.com/questions/1848700/biggest-integer-that-can-be-stored-in-a-double#1848762)). Meaning if you go above that, the difference between successive integers is 2. And in a 32-bit float, 2^24. So, I can't answer your original questions, but I think if you only have 64-bit floats (doubles), you will still have to split the 64-bit integers into two Lua numbers. – cyclaminist Apr 19 '18 at 16:40

1 Answers1

4

After more detailed investigation regarding this topic, testing and communication with other people which opinion is relevant to this subject, posting an answer:

In this specific case, it really does not matter do you have 32 bit or 64 bit compiler (or device). The biggest integer you are able to represent based on lua doc: 64-bit floats can only represent integers up to 53 bits of precision, and the hex number I used as an example is bigger then that.

This means that any kind of general bit-wise operations are not going to work for these numbers.

Basically, this question can be reconstructed in: "How I can use bit wise operations in lua on full 64 bits?".

Only solution is to use libraries for this one or to implement it by yourself (separating 64 bit number to two 32 bit numbers and implementing bit wise operators by yourself)

cool
  • 3,225
  • 3
  • 33
  • 58