Skip to content

Commit 3b942d1

Browse files
committed
Bits and bytes
1 parent f2e5287 commit 3b942d1

File tree

1 file changed

+126
-9
lines changed

1 file changed

+126
-9
lines changed

content/micropython/01.basics/08.reference/reference.md

Lines changed: 126 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,132 @@ while True:
232232
switch = not switch
233233
ledPin.value(switch)
234234
```
235+
236+
## Bits and Bytes
237+
238+
### bitRead()
239+
240+
`bit_value = (variable >> bit_index) & 1`
241+
242+
Reads a specific bit of an integer variable.
243+
244+
**Parameters:**
245+
- `variable` - an integer variable with numeric value, e.g. `12345`
246+
- `bit_index` - the specific bit we want to read (e.g. `5`)
247+
248+
**Returns:**
249+
- The value of the specific bit.
250+
251+
**Example:**
252+
253+
```python
254+
'''
255+
This example prints out each bit of an 8-bit variable
256+
It stops after 255 (the max value an 8-bit variable can hold)
257+
'''
258+
import time
259+
counter = 0
260+
261+
bit_length = 8 # 8 bits
262+
while True:
263+
bits = []
264+
for bit_index in range(bit_length - 1, -1, -1):
265+
bit_value = (counter >> bit_index) & 1
266+
bits.append(bit_value)
267+
print("Binary: ", bits, " DEC: ", counter)
268+
269+
counter += 1
270+
time.sleep(0.01)
271+
if counter > 255:
272+
break
273+
```
274+
275+
### bitSet()
276+
277+
`variable = variable | (1 << bit_index)`
278+
279+
Sets a specific bit of an integer variable.
280+
281+
**Parameters:**
282+
- `variable` - an integer variable with numeric value, e.g. `12345`
283+
- `bit_index` - the specific bit we want to read (e.g. `5`)
284+
285+
**Returns:**
286+
- Nothing.
287+
288+
**Example:**
289+
290+
```python
291+
# Example variable
292+
variable = 12345
293+
294+
# Set the third bit
295+
bit_index = 2
296+
297+
print()
298+
print("Before setting a bit: ",bin(variable))
299+
print("Before setting a bit: ",variable)
300+
variable = variable | (1 << bit_index)
301+
302+
# Print the result
303+
print("After setting a bit: ",bin(variable))
304+
print("After setting a bit: ",variable)
305+
```
306+
307+
### highByte()
308+
309+
`leftmost_bit = (variable >> leftmost_bit_index) & 1`
310+
311+
Reads the high-order (leftmost) bit of a variable.
312+
313+
**Parameters**
314+
- `variable` - an integer variable with numeric value, e.g. `255`
315+
- `leftmost_bit_index` - the leftmost bit, e.g. `7` in an 8-bit variable.
316+
317+
**Returns**
318+
- `leftmost_bit` - the value of the leftmost bit.
319+
320+
**Example:**
321+
322+
```python
323+
# Example variable
324+
variable = 255
325+
326+
bit_length = 8
327+
# Extract the leftmost bit
328+
leftmost_bit_index = bit_length - 1
329+
leftmost_bit = (variable >> leftmost_bit_index) & 1
330+
331+
# Print the result
332+
print("Leftmost bit: ", leftmost_bit)
333+
```
334+
335+
### lowByte()
336+
337+
`rightmost_bit = variable & 1`
338+
339+
Reads the low-order (rightmost) bit of a variable.
340+
341+
**Parameters**
342+
- `variable` - an integer variable with numeric value, e.g. `255`
343+
- `rightmost_bit` - the rightmost bit, `0` in an 8-bit variable.
344+
345+
**Returns**
346+
- `rightmost_bit` - the value of the rightmost bit, e.g. `1` if `variable` is 255 (255 is 11111111 in binary).
347+
348+
**Example:**
349+
350+
```python
351+
# Example variable
352+
variable = 255
353+
354+
# Extract the rightmost bit
355+
rightmost_bit = variable & 1
356+
357+
# Print the result
358+
print("Rigthmost bit: ", rightmost_bit)
359+
```
360+
235361
<!-- Section not started yet
236362
## Advanced I/O
237363
@@ -280,15 +406,6 @@ Random Numbers
280406
## random()
281407
## randomSeed()
282408
283-
Bits and Bytes
284-
285-
### bit()
286-
### bitClear()
287-
### bitRead()
288-
### bitSet()
289-
### bitWrite()
290-
### highByte()
291-
### lowByte()
292409
293410
External Interrupts
294411

0 commit comments

Comments
 (0)