You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Language/Functions/Bits and Bytes/bitRead.adoc
+49-1
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,7 @@ subCategories: [ "Bits and Bytes" ]
17
17
18
18
[float]
19
19
=== Description
20
-
Reads a bit of a number.
20
+
Reads a bit of a variable, e.g. `bool`, `int`. Note that `float` & `double` are not supported. You can read the bit of variables up to an `unsigned long long` (64 bits / 8 bytes).
21
21
[%hardbreaks]
22
22
23
23
@@ -36,6 +36,54 @@ Reads a bit of a number.
36
36
=== Returns
37
37
The value of the bit (0 or 1).
38
38
39
+
=== Example Code
40
+
41
+
This example code demonstrates how to read two variables, one increasing counter, one decreasing counter, and print out both the binary and decimal values of the variables.
42
+
43
+
The `readBit()` function loops through each bit of the variable (starting from the rightmost bit), and prints it out.
44
+
45
+
[source,arduino]
46
+
----
47
+
long negative_var = -0; //
48
+
unsigned long long positive_var = 0;
49
+
50
+
//predefined sizes when looping through bits
51
+
//e.g. long_size is 32 bit (which is 0-31). Therefore, we subtract "1".
52
+
const int bool_size = (1 - 1);
53
+
const int int_size = (8 - 1);
54
+
const int long_size = (32 - 1);
55
+
56
+
void setup() {
57
+
Serial.begin(9600);
58
+
}
59
+
60
+
void loop() {
61
+
//run readBit function, passing the pos/neg variables
62
+
readBit("Positive ", positive_var);
63
+
readBit("Negative ", negative_var);
64
+
Serial.println();
65
+
66
+
//increase and decrease the variables
67
+
negative_var--;
68
+
positive_var++;
69
+
70
+
delay(1000);
71
+
}
72
+
73
+
/*this function takes a variable, prints it out bit by bit (starting from the right)
Copy file name to clipboardExpand all lines: Language/Functions/Bits and Bytes/bitWrite.adoc
+1-1
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,7 @@ subCategories: [ "Bits and Bytes" ]
17
17
18
18
[float]
19
19
=== Description
20
-
Writes a bit of a numeric variable.
20
+
Writes to a bit of a variable, e.g. `bool`, `int`, `long`. Note that `float` & `double` are not supported. You can write to a bit of variables up to an `unsigned long` (32 bits / 8 bytes).
0 commit comments