|
2 | 2 | ---@diagnostic disable:lowercase-global
|
3 | 3 | ---@diagnostic disable:duplicate-set-field
|
4 | 4 |
|
5 |
| --- The Bit API provides functions for performing bitwise operations on integer numbers, the Bit API is from the [LuaJIT BitOp library](http://bitop.luajit.org/index.html), documentation is copied from [here](http://bitop.luajit.org/api.html)<br> |
| 5 | +-- The Bit API provides functions for performing bitwise operations on integer numbers, the Bit API is from the [LuaJIT BitOp library](http://bitop.luajit.org/index.html), documentation for it is [here](http://bitop.luajit.org/api.html)<br> |
6 | 6 | -- If you are unfamiliar with bitwise operations, you may want to check the Wikipedia page on the subject: [http://en.wikipedia.org/wiki/Bitwise_operation](http://en.wikipedia.org/wiki/Bitwise_operation)
|
7 | 7 | bit = {}
|
8 | 8 |
|
| 9 | + |
9 | 10 | --```
|
10 | 11 | --bit.tobit(number input)
|
11 | 12 | --```
|
12 |
| --- Turns a number into something usable by the rest of the binary utility functions.<br> |
13 |
| --- It converts the number into a better form (what C deems to be a signed 32-bit int) to be used by the other bit functions. If you call one of the functions below yourself however, you do not need to use this function, as the below functions already use this in their code. |
14 |
| --- **Examples:** |
| 13 | +--Converts a 64-bit integer into a 32-bit integer by removing all bits above 32nd. You do not need to use this function, as the below functions will do this automatically. Here's some example values:<br> |
15 | 14 | --```
|
16 |
| --- bit.tobit(100) --> 100 |
17 |
| --- bit.tobit(-100) --> -100 |
18 |
| --- bit.tobit(2147483647) --> bit.tobit(2^31 - 1) = 2147483647 |
19 |
| --- bit.tobit(2147483648) --> bit.tobit(2^31) = -2147483648 |
| 15 | +--bit.tobit(100) --> 100 |
| 16 | +--bit.tobit(-100) --> -100 |
| 17 | +--bit.tobit(2147483647) --> bit.tobit(2^31 - 1) = 2147483647 |
| 18 | +--bit.tobit(2147483648) --> bit.tobit(2^31) = -2147483648 |
20 | 19 | --```
|
21 | 20 | ---@param input integer
|
22 | 21 | ---@return integer
|
|
64 | 63 | --bit.band(number input, [number input...])
|
65 | 64 | --```
|
66 | 65 | --Returns the binary AND of all its arguments combined.<br>
|
67 |
| ---A binary AND is an operation that requires two numbers: the only bits that remain in the return value are ones that occur in both of the numbers. This is an easy way to pick out a single byte from a "bit string" - an integer that is actually used as a row of bits that can be used as boolean values. You can then store 32 booleans in a single int, whereas usually a single boolean is a single int! |
| 66 | +--A binary AND is an operation that requires two numbers: the only bits that remain in the return value are ones that occur in both of the numbers. This is an easy way to pick out a single bit from a "bit string" - an integer that is actually used as a row of bits that can be used as boolean values. You can then store 8 booleans in a single byte, whereas usually a single boolean is a single byte! |
68 | 67 | -- **Example:**
|
69 | 68 | --```
|
70 | 69 | -- :: 0001 1001 &
|
|
117 | 116 | --```
|
118 | 117 | -- Shifts the input bits to the left by (shift) bits, replacing empty cells with 0 and discarding overflowing cells.<br>
|
119 | 118 | -- Left shifting is something really easy to imagine: every bit just gets moved once (or more) to the left and the ends are replaced with 0. If you have a bit at the far left end it gets eaten and is lost forever. Not to worry usually though.<br>
|
120 |
| --- Another great utilization of left shifting is that it's a really quick way of multiplying a number by two for some times. Shifting to the left by two is the same as multiplying a number by four.<br> |
| 119 | +-- Another great utilization of left shifting is that it's a really quick way of multiplying a number by two for some times. Shifting to the left by two is the same as multiplying a number by four. Note that this performs logical shift meaning that it will also move the *sign bit*<br> |
121 | 120 | --```
|
122 | 121 | -- :: 0000 0010 << 2
|
123 | 122 | -- = 0000 1000
|
|
139 | 138 | -- ```
|
140 | 139 | -- Shifts the input bits to the right by (shift) bits, replacing empty cells with 0 and discarding overflowing cells.<br>
|
141 | 140 | -- Right shifting is something really easy to imagine: every bit just gets moved once (or more) to the right and the ends are replaced with 0. If you have a bit at the far right end it gets eaten and is lost forever. Not to worry usually though.<br>
|
142 |
| --- Another great utilization of right shifting is that it's a really quick way of dividing a number by two for some times. Shifting to the right by two is the same as dividing a number by four. Note that this will also move the sign bit. Which is what arithmetic right shift does not in fact do. |
| 141 | +-- Another great utilization of right shifting is that it's a really quick way of dividing a number by two for some times. Shifting to the right by two is the same as dividing a number by four. Note that this a performs logical shift meaning that it will also move the sign bit. |
143 | 142 | -- ```
|
144 | 143 | -- 1111 1111 :: >> 2
|
145 | 144 | -- = 0011 1111 1100 0000 ::
|
|
161 | 160 | -- bit.arshift(number input, number shift)
|
162 | 161 | -- ```
|
163 | 162 | -- Shifts the input bits to the right by (shift) bits, copying over the sign bit for the new bits added from the left.<br>
|
164 |
| --- So the solution to our right shifting problem is that we need to add ones instead of zeros to the left side if we shift from the right. This keeps the sign right in place and still allows us to shift all we want.<br> |
| 163 | +-- This keeps the sign right in place and still allows us to shift all we want.<br> |
165 | 164 | -- ```
|
166 | 165 | -- 1001 1001 0000 0000 :: >>> 2
|
167 | 166 | -- = 1110 0110 0100 0000 ::
|
|
221 | 220 | -- ```
|
222 | 221 | -- bit.bswap(number input)
|
223 | 222 | -- ```
|
224 |
| --- Swaps the byte order of the argument. |
| 223 | +-- Swaps the *byte order* of the argument. |
225 | 224 | --<br>
|
226 |
| --- In a few places, TPT uses weird reverse-byte-order numbers, such as TPT saves in the OPS 1 format. This function makes things easier and just flips the bytes for you. |
| 225 | +-- In some places, TPT uses low-endian (least significant byte on the left) instead of usually used big-endian (most significant byte on the left) order of bytes, such as in TPT saves in the OPS 1 format. This function makes things easier and just flips the byte order for you. |
227 | 226 | -- ```
|
228 | 227 | -- 0110 1010 0101 1001
|
229 | 228 | -- = 1001 0101 1010 0110
|
|
0 commit comments