nodemcu-firmware/docs/modules/bit.md

177 lines
3.6 KiB
Markdown
Raw Normal View History

2016-11-17 11:56:51 +01:00
# bit Module
| Since | Origin / Contributor | Maintainer | Source |
| :----- | :-------------------- | :---------- | :------ |
2019-01-16 23:31:09 +01:00
| 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](../../components/modules/bit.c)|
2016-11-17 11:56:51 +01:00
Bit manipulation support, on the integer type used by the Lua VM. On 5.1 this
will be 32bit, whereas on 5.3 it may be either 32bit (default) or 64bit if
configured for 64bit integers.
2016-11-17 11:56:51 +01:00
## bit.arshift()
Arithmetic right shift a number equivalent to `value >> shift` in C.
#### Syntax
2016-11-17 11:56:51 +01:00
`bit.arshift(value, shift)`
#### Parameters
2016-11-17 11:56:51 +01:00
- `value` the value to shift
- `shift` positions to shift
#### Returns
2016-11-17 11:56:51 +01:00
the number shifted right (arithmetically)
## bit.band()
Bitwise AND, equivalent to `val1 & val2 & ... & valn` in C.
#### Syntax
2016-11-17 11:56:51 +01:00
`bit.band(val1, val2 [, ... valn])`
#### Parameters
- `val1` first AND argument
- `val2` second AND argument
- `...valn` ...nth AND argument
2016-11-17 11:56:51 +01:00
#### Returns
2016-11-17 11:56:51 +01:00
the bitwise AND of all the arguments (number)
## bit.bit()
Generate a number with a 1 bit (used for mask generation). Equivalent to `1 << position` in C.
#### Syntax
2016-11-17 11:56:51 +01:00
`bit.bit(position)`
#### Parameters
2016-11-17 11:56:51 +01:00
`position` position of the bit that will be set to 1
#### Returns
2016-11-17 11:56:51 +01:00
a number with only one 1 bit at position (the rest are set to 0)
## bit.bnot()
Bitwise negation, equivalent to `~value in C.
#### Syntax
2016-11-17 11:56:51 +01:00
`bit.bnot(value)`
#### Parameters
2016-11-17 11:56:51 +01:00
`value` the number to negate
#### Returns
2016-11-17 11:56:51 +01:00
the bitwise negated value of the number
## bit.bor()
Bitwise OR, equivalent to `val1 | val2 | ... | valn` in C.
#### Syntax
2016-11-17 11:56:51 +01:00
`bit.bor(val1, val2 [, ... valn])`
#### Parameters
2016-11-17 11:56:51 +01:00
- `val1` first OR argument.
- `val2` second OR argument.
- `...valn` ...nth OR argument
#### Returns
2016-11-17 11:56:51 +01:00
the bitwise OR of all the arguments (number)
## bit.bxor()
Bitwise XOR, equivalent to `val1 ^ val2 ^ ... ^ valn` in C.
#### Syntax
2016-11-17 11:56:51 +01:00
`bit.bxor(val1, val2 [, ... valn])`
#### Parameters
2016-11-17 11:56:51 +01:00
- `val1` first XOR argument
- `val2` second XOR argument
- `...valn` ...nth XOR argument
#### Returns
2016-11-17 11:56:51 +01:00
the bitwise XOR of all the arguments (number)
## bit.clear()
Clear bits in a number.
#### Syntax
2016-11-17 11:56:51 +01:00
`bit.clear(value, pos1 [, ... posn])`
#### Parameters
2016-11-17 11:56:51 +01:00
- `value` the base number
- `pos1` position of the first bit to clear
- `...posn` position of thet nth bit to clear
#### Returns
2016-11-17 11:56:51 +01:00
the number with the bit(s) cleared in the given position(s)
## bit.isclear()
Test if a given bit is cleared.
#### Syntax
2016-11-17 11:56:51 +01:00
`bit.isclear(value, position)`
#### Parameters
2016-11-17 11:56:51 +01:00
- `value` the value to test
- `position` bit position to test
#### Returns
`true` if the bit at the given position is 0, `false` othewise
2016-11-17 11:56:51 +01:00
## bit.isset()
Test if a given bit is set.
#### Syntax
2016-11-17 11:56:51 +01:00
`bit.isset(value, position)`
#### Parameters
2016-11-17 11:56:51 +01:00
- `value` the value to test
- `position` bit position to test
#### Returns
`true` if the bit at the given position is 1, `false` otherwise
2016-11-17 11:56:51 +01:00
## bit.lshift()
Left-shift a number, equivalent to `value << shift` in C.
#### Syntax
2016-11-17 11:56:51 +01:00
`bit.lshift(value, shift)`
#### Parameters
2016-11-17 11:56:51 +01:00
- `value` the value to shift
- `shift` positions to shift
#### Returns
2016-11-17 11:56:51 +01:00
the number shifted left
## bit.rshift()
Logical right shift a number, equivalent to `( unsigned )value >> shift` in C.
#### Syntax
2016-11-17 11:56:51 +01:00
`bit.rshift(value, shift)`
#### Parameters
2016-11-17 11:56:51 +01:00
- `value` the value to shift.
- `shift` positions to shift.
#### Returns
2016-11-17 11:56:51 +01:00
the number shifted right (logically)
## bit.set()
Set bits in a number.
#### Syntax
2016-11-17 11:56:51 +01:00
`bit.set(value, pos1 [, ... posn ])`
#### Parameters
2016-11-17 11:56:51 +01:00
- `value` the base number.
- `pos1` position of the first bit to set.
- `...posn` position of the nth bit to set.
#### Returns
2016-11-17 11:56:51 +01:00
the number with the bit(s) set in the given position(s)