Add examples to the "bit" module documentation (#2528)

This commit is contained in:
Adriano Melo 2018-11-03 21:45:08 +01:00 committed by Marcel Stör
parent 5767475766
commit f5e68157a1
1 changed files with 62 additions and 2 deletions

View File

@ -19,6 +19,12 @@ Arithmetic right shift a number equivalent to `value >> shift` in C.
#### Returns #### Returns
the number shifted right (arithmetically) the number shifted right (arithmetically)
#### Example
```lua
bit.arshift(3, 1) -- returns 1
-- Using a 4 bits representation: 0011 >> 1 == 0001
```
## bit.band() ## bit.band()
Bitwise AND, equivalent to `val1 & val2 & ... & valn` in C. Bitwise AND, equivalent to `val1 & val2 & ... & valn` in C.
@ -34,6 +40,12 @@ Bitwise AND, equivalent to `val1 & val2 & ... & valn` in C.
#### Returns #### Returns
the bitwise AND of all the arguments (number) the bitwise AND of all the arguments (number)
### Example
```lua
bit.band(3, 2) -- returns 2
-- Using a 4 bits representation: 0011 & 0010 == 0010
```
## bit.bit() ## bit.bit()
Generate a number with a 1 bit (used for mask generation). Equivalent to `1 << position` in C. Generate a number with a 1 bit (used for mask generation). Equivalent to `1 << position` in C.
@ -47,6 +59,11 @@ Generate a number with a 1 bit (used for mask generation). Equivalent to `1 << p
#### Returns #### Returns
a number with only one 1 bit at position (the rest are set to 0) a number with only one 1 bit at position (the rest are set to 0)
### Example
```lua
bit.bit(4) -- returns 16
```
## bit.bnot() ## bit.bnot()
Bitwise negation, equivalent to `~value in C.` Bitwise negation, equivalent to `~value in C.`
@ -74,6 +91,12 @@ Bitwise OR, equivalent to `val1 | val2 | ... | valn` in C.
#### Returns #### Returns
the bitwise OR of all the arguments (number) the bitwise OR of all the arguments (number)
### Example
```lua
bit.bor(3, 2) -- returns 3
-- Using a 4 bits representation: 0011 | 0010 == 0011
```
## bit.bxor() ## bit.bxor()
Bitwise XOR, equivalent to `val1 ^ val2 ^ ... ^ valn` in C. Bitwise XOR, equivalent to `val1 ^ val2 ^ ... ^ valn` in C.
@ -89,6 +112,12 @@ Bitwise XOR, equivalent to `val1 ^ val2 ^ ... ^ valn` in C.
#### Returns #### Returns
the bitwise XOR of all the arguments (number) the bitwise XOR of all the arguments (number)
### Example
```lua
bit.bxor(3, 2) -- returns 1
-- Using a 4 bits representation: 0011 ^ 0010 == 0001
```
## bit.clear() ## bit.clear()
Clear bits in a number. Clear bits in a number.
@ -103,8 +132,12 @@ Clear bits in a number.
#### Returns #### Returns
the number with the bit(s) cleared in the given position(s) the number with the bit(s) cleared in the given position(s)
## bit.isclear() ### Example
```lua
bit.clear(3, 0) -- returns 2
```
## bit.isclear()
Test if a given bit is cleared. Test if a given bit is cleared.
#### Syntax #### Syntax
@ -117,6 +150,11 @@ Test if a given bit is cleared.
#### Returns #### Returns
true if the bit at the given position is 0, false othewise true if the bit at the given position is 0, false othewise
### Example
```lua
bit.isclear(2, 0) -- returns true
```
## bit.isset() ## bit.isset()
Test if a given bit is set. Test if a given bit is set.
@ -131,6 +169,11 @@ Test if a given bit is set.
#### Returns #### Returns
true if the bit at the given position is 1, false otherwise true if the bit at the given position is 1, false otherwise
### Example
```lua
bit.isset(2, 0) -- returns false
```
## bit.lshift() ## bit.lshift()
Left-shift a number, equivalent to `value << shift` in C. Left-shift a number, equivalent to `value << shift` in C.
@ -144,6 +187,12 @@ Left-shift a number, equivalent to `value << shift` in C.
#### Returns #### Returns
the number shifted left the number shifted left
### Example
```lua
bit.lshift(2, 2) -- returns 8
-- Using a 4 bits representation: 0010 << 2 == 1000
```
## bit.rshift() ## bit.rshift()
Logical right shift a number, equivalent to `( unsigned )value >> shift` in C. Logical right shift a number, equivalent to `( unsigned )value >> shift` in C.
@ -158,6 +207,12 @@ Logical right shift a number, equivalent to `( unsigned )value >> shift` in C.
#### Returns #### Returns
the number shifted right (logically) the number shifted right (logically)
### Example
```lua
bit.rshift(2, 1) -- returns 1
-- Using a 4 bits representation: 0010 >> 1 == 0001
```
## bit.set() ## bit.set()
Set bits in a number. Set bits in a number.
@ -171,4 +226,9 @@ Set bits in a number.
- `...posn` position of the nth bit to set. - `...posn` position of the nth bit to set.
#### Returns #### Returns
the number with the bit(s) set in the given position(s) the number with the bit(s) set in the given position(s)
### Example
```lua
bit.set(2, 0) -- returns 3
```