2016-01-05 06:07:29 +01:00
|
|
|
# bit Module
|
2016-03-05 10:47:01 +01:00
|
|
|
| Since | Origin / Contributor | Maintainer | Source |
|
|
|
|
| :----- | :-------------------- | :---------- | :------ |
|
|
|
|
| 2014-12-24 | [https://github.com/LuaDist/bitlib](https://github.com/LuaDist/bitlib), [Zeroday](https://github.com/funshine) | [Zeroday](https://github.com/funshine) | [bit.c](../../../app/modules/bit.c)|
|
|
|
|
|
2016-01-05 06:07:29 +01:00
|
|
|
|
|
|
|
Bit manipulation support, on 32bit integers.
|
|
|
|
|
2016-01-08 20:49:56 +01:00
|
|
|
## bit.arshift()
|
|
|
|
Arithmetic right shift a number equivalent to `value >> shift` in C.
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Syntax
|
2016-01-08 20:49:56 +01:00
|
|
|
`bit.arshift(value, shift)`
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Parameters
|
2016-01-08 20:49:56 +01:00
|
|
|
- `value` the value to shift
|
|
|
|
- `shift` positions to shift
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Returns
|
2016-01-08 20:49:56 +01:00
|
|
|
the number shifted right (arithmetically)
|
|
|
|
|
2018-11-03 21:45:08 +01:00
|
|
|
#### Example
|
|
|
|
```lua
|
|
|
|
bit.arshift(3, 1) -- returns 1
|
|
|
|
-- Using a 4 bits representation: 0011 >> 1 == 0001
|
|
|
|
```
|
|
|
|
|
2016-01-05 06:07:29 +01:00
|
|
|
## bit.band()
|
|
|
|
|
2016-01-08 20:49:56 +01:00
|
|
|
Bitwise AND, equivalent to `val1 & val2 & ... & valn` in C.
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Syntax
|
2016-01-05 06:07:29 +01:00
|
|
|
`bit.band(val1, val2 [, ... valn])`
|
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Parameters
|
2016-01-08 20:49:56 +01:00
|
|
|
- `val1` first AND argument
|
|
|
|
- `val2` second AND argument
|
|
|
|
- `...valn` ...nth AND argument
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Returns
|
2016-01-08 20:49:56 +01:00
|
|
|
the bitwise AND of all the arguments (number)
|
|
|
|
|
2018-11-03 21:45:08 +01:00
|
|
|
### Example
|
|
|
|
```lua
|
|
|
|
bit.band(3, 2) -- returns 2
|
|
|
|
-- Using a 4 bits representation: 0011 & 0010 == 0010
|
|
|
|
```
|
|
|
|
|
2016-01-08 20:49:56 +01:00
|
|
|
## bit.bit()
|
|
|
|
|
|
|
|
Generate a number with a 1 bit (used for mask generation). Equivalent to `1 << position` in C.
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Syntax
|
2016-01-08 20:49:56 +01:00
|
|
|
`bit.bit(position)`
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Parameters
|
2016-01-08 20:49:56 +01:00
|
|
|
`position` position of the bit that will be set to 1
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Returns
|
2016-01-08 20:49:56 +01:00
|
|
|
a number with only one 1 bit at position (the rest are set to 0)
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2018-11-03 21:45:08 +01:00
|
|
|
### Example
|
|
|
|
```lua
|
|
|
|
bit.bit(4) -- returns 16
|
|
|
|
```
|
|
|
|
|
2016-01-08 20:49:56 +01:00
|
|
|
## bit.bnot()
|
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
Bitwise negation, equivalent to `~value in C.`
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Syntax
|
2016-01-08 20:49:56 +01:00
|
|
|
`bit.bnot(value)`
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Parameters
|
2016-01-08 20:49:56 +01:00
|
|
|
`value` the number to negate
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Returns
|
2016-01-08 20:49:56 +01:00
|
|
|
the bitwise negated value of the number
|
|
|
|
|
|
|
|
## bit.bor()
|
|
|
|
Bitwise OR, equivalent to `val1 | val2 | ... | valn` in C.
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Syntax
|
2016-01-08 20:49:56 +01:00
|
|
|
`bit.bor(val1, val2 [, ... valn])`
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Parameters
|
2016-01-08 20:49:56 +01:00
|
|
|
- `val1` first OR argument.
|
|
|
|
- `val2` second OR argument.
|
|
|
|
- `...valn` ...nth OR argument
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Returns
|
2016-01-08 20:49:56 +01:00
|
|
|
the bitwise OR of all the arguments (number)
|
|
|
|
|
2018-11-03 21:45:08 +01:00
|
|
|
### Example
|
|
|
|
```lua
|
|
|
|
bit.bor(3, 2) -- returns 3
|
|
|
|
-- Using a 4 bits representation: 0011 | 0010 == 0011
|
|
|
|
```
|
|
|
|
|
2016-01-08 20:49:56 +01:00
|
|
|
## bit.bxor()
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2016-01-08 20:49:56 +01:00
|
|
|
Bitwise XOR, equivalent to `val1 ^ val2 ^ ... ^ valn` in C.
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Syntax
|
2016-01-08 20:49:56 +01:00
|
|
|
`bit.bxor(val1, val2 [, ... valn])`
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Parameters
|
2016-01-08 20:49:56 +01:00
|
|
|
- `val1` first XOR argument
|
|
|
|
- `val2` second XOR argument
|
|
|
|
- `...valn` ...nth XOR argument
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Returns
|
2016-01-08 20:49:56 +01:00
|
|
|
the bitwise XOR of all the arguments (number)
|
|
|
|
|
2018-11-03 21:45:08 +01:00
|
|
|
### Example
|
|
|
|
```lua
|
|
|
|
bit.bxor(3, 2) -- returns 1
|
|
|
|
-- Using a 4 bits representation: 0011 ^ 0010 == 0001
|
|
|
|
```
|
|
|
|
|
2016-01-08 20:49:56 +01:00
|
|
|
## bit.clear()
|
|
|
|
Clear bits in a number.
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Syntax
|
2016-01-08 20:49:56 +01:00
|
|
|
`bit.clear(value, pos1 [, ... posn])`
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Parameters
|
2016-01-08 20:49:56 +01:00
|
|
|
- `value` the base number
|
|
|
|
- `pos1` position of the first bit to clear
|
|
|
|
- `...posn` position of thet nth bit to clear
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Returns
|
2016-01-08 20:49:56 +01:00
|
|
|
the number with the bit(s) cleared in the given position(s)
|
|
|
|
|
2018-11-03 21:45:08 +01:00
|
|
|
### Example
|
|
|
|
```lua
|
|
|
|
bit.clear(3, 0) -- returns 2
|
|
|
|
```
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2018-11-03 21:45:08 +01:00
|
|
|
## bit.isclear()
|
2016-01-08 20:49:56 +01:00
|
|
|
Test if a given bit is cleared.
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Syntax
|
2016-01-08 20:49:56 +01:00
|
|
|
`bit.isclear(value, position)`
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Parameters
|
2016-01-08 20:49:56 +01:00
|
|
|
- `value` the value to test
|
|
|
|
- `position` bit position to test
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Returns
|
2016-01-08 20:49:56 +01:00
|
|
|
true if the bit at the given position is 0, false othewise
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2018-11-03 21:45:08 +01:00
|
|
|
### Example
|
|
|
|
```lua
|
|
|
|
bit.isclear(2, 0) -- returns true
|
|
|
|
```
|
|
|
|
|
2016-01-08 20:49:56 +01:00
|
|
|
## bit.isset()
|
|
|
|
|
|
|
|
Test if a given bit is set.
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Syntax
|
2016-01-08 20:49:56 +01:00
|
|
|
`bit.isset(value, position)`
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Parameters
|
2016-01-08 20:49:56 +01:00
|
|
|
- `value` the value to test
|
|
|
|
- `position` bit position to test
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Returns
|
2016-01-08 20:49:56 +01:00
|
|
|
true if the bit at the given position is 1, false otherwise
|
|
|
|
|
2018-11-03 21:45:08 +01:00
|
|
|
### Example
|
|
|
|
```lua
|
|
|
|
bit.isset(2, 0) -- returns false
|
|
|
|
```
|
|
|
|
|
2016-01-08 20:49:56 +01:00
|
|
|
## bit.lshift()
|
|
|
|
Left-shift a number, equivalent to `value << shift` in C.
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Syntax
|
2016-01-08 20:49:56 +01:00
|
|
|
`bit.lshift(value, shift)`
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Parameters
|
2016-01-08 20:49:56 +01:00
|
|
|
- `value` the value to shift
|
|
|
|
- `shift` positions to shift
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Returns
|
2016-01-08 20:49:56 +01:00
|
|
|
the number shifted left
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2018-11-03 21:45:08 +01:00
|
|
|
### Example
|
|
|
|
```lua
|
|
|
|
bit.lshift(2, 2) -- returns 8
|
|
|
|
-- Using a 4 bits representation: 0010 << 2 == 1000
|
|
|
|
```
|
|
|
|
|
2016-01-08 20:49:56 +01:00
|
|
|
## bit.rshift()
|
|
|
|
|
|
|
|
Logical right shift a number, equivalent to `( unsigned )value >> shift` in C.
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Syntax
|
2016-01-08 20:49:56 +01:00
|
|
|
`bit.rshift(value, shift)`
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Parameters
|
2016-01-08 20:49:56 +01:00
|
|
|
- `value` the value to shift.
|
|
|
|
- `shift` positions to shift.
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Returns
|
2016-01-08 20:49:56 +01:00
|
|
|
the number shifted right (logically)
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2018-11-03 21:45:08 +01:00
|
|
|
### Example
|
|
|
|
```lua
|
|
|
|
bit.rshift(2, 1) -- returns 1
|
|
|
|
-- Using a 4 bits representation: 0010 >> 1 == 0001
|
|
|
|
```
|
|
|
|
|
2016-01-08 20:49:56 +01:00
|
|
|
## bit.set()
|
|
|
|
|
|
|
|
Set bits in a number.
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Syntax
|
2016-01-08 20:49:56 +01:00
|
|
|
`bit.set(value, pos1 [, ... posn ])`
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Parameters
|
2016-01-08 20:49:56 +01:00
|
|
|
- `value` the base number.
|
|
|
|
- `pos1` position of the first bit to set.
|
|
|
|
- `...posn` position of the nth bit to set.
|
2016-01-05 06:07:29 +01:00
|
|
|
|
2017-08-31 22:46:57 +02:00
|
|
|
#### Returns
|
2018-11-03 21:45:08 +01:00
|
|
|
the number with the bit(s) set in the given position(s)
|
|
|
|
|
|
|
|
### Example
|
|
|
|
```lua
|
|
|
|
bit.set(2, 0) -- returns 3
|
|
|
|
```
|