Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
Nodemcu Firmware
Commits
f5e68157
Commit
f5e68157
authored
Nov 03, 2018
by
Adriano Melo
Committed by
Marcel Stör
Nov 03, 2018
Browse files
Add examples to the "bit" module documentation (#2528)
parent
57674757
Changes
1
Hide whitespace changes
Inline
Side-by-side
docs/en/modules/bit.md
View file @
f5e68157
...
@@ -19,6 +19,12 @@ Arithmetic right shift a number equivalent to `value >> shift` in C.
...
@@ -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.
...
@@ -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
...
@@ -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.
...
@@ -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.
...
@@ -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.
...
@@ -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.
...
@@ -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.
...
@@ -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.
...
@@ -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.
...
@@ -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.
...
@@ -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)
\ No newline at end of file
### Example
```
lua
bit
.
set
(
2
,
0
)
-- returns 3
```
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment