Skip to content

Commit 4d562db

Browse files
Replace "B..." binary constants with "0b..." (#793)
Replaced all references to the deprecated "B..." binary format (e.g. B00101010) with the GCC- and C++-compliant "0b..." format (e.g. 0b00101010). This also removes the restriction that binary constants must be 8 bits or less, so I'm removing that part as well.
1 parent 1d25fd2 commit 4d562db

File tree

7 files changed

+26
-32
lines changed

7 files changed

+26
-32
lines changed

Language/Structure/Bitwise Operators/bitwiseAnd.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ One of the most common uses of bitwise AND is to select a particular bit (or bit
5858

5959
[source,arduino]
6060
----
61-
PORTD = PORTD & B00000011; // clear out bits 2 - 7, leave pins PD0 and PD1 untouched (xx & 11 == xx)
61+
PORTD = PORTD & 0b00000011; // clear out bits 2 - 7, leave pins PD0 and PD1 untouched (xx & 11 == xx)
6262
----
6363

6464
--

Language/Structure/Bitwise Operators/bitwiseOr.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ One of the most common uses of the Bitwise OR is to set multiple bits in a bit-p
5656
// Note: This code is AVR architecture specific
5757
// set direction bits for pins 2 to 7, leave PD0 and PD1 untouched (xx | 00 == xx)
5858
// same as pinMode(pin, OUTPUT) for pins 2 to 7 on Uno or Nano
59-
DDRD = DDRD | B11111100;
59+
DDRD = DDRD | 0b11111100;
6060
----
6161

6262
--

Language/Structure/Bitwise Operators/bitwiseXor.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ The ^ operator is often used to toggle (i.e. change from 0 to 1, or 1 to 0) some
5656
// Note: This code uses registers specific to AVR microcontrollers (Uno, Nano, Leonardo, Mega, etc.)
5757
// it will not compile for other architectures
5858
void setup() {
59-
DDRB = DDRB | B00100000; // set PB5 (pin 13 on Uno/Nano, pin 9 on Leonardo/Micro, pin 11 on Mega) as OUTPUT
59+
DDRB = DDRB | 0b00100000; // set PB5 (pin 13 on Uno/Nano, pin 9 on Leonardo/Micro, pin 11 on Mega) as OUTPUT
6060
Serial.begin(9600);
6161
}
6262
6363
void loop() {
64-
PORTB = PORTB ^ B00100000; // invert PB5, leave others untouched
64+
PORTB = PORTB ^ 0b00100000; // invert PB5, leave others untouched
6565
delay(100);
6666
}
6767
----

Language/Structure/Compound Operators/compoundBitwiseAnd.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,22 @@ Bits that are "bitwise ANDed" with 0 are cleared to 0 so, if myByte is a byte va
5454

5555
[source,arduino]
5656
----
57-
myByte & B00000000 = 0;
57+
myByte & 0b00000000 = 0;
5858
----
5959

6060
Bits that are "bitwise ANDed" with 1 are unchanged so,
6161

6262
[source,arduino]
6363
----
64-
myByte & B11111111 = myByte;
64+
myByte & 0b11111111 = myByte;
6565
----
6666
[%hardbreaks]
6767

6868
[float]
6969
=== Notes and Warnings
70-
Because we are dealing with bits in a bitwise operator - it is convenient to use the binary formatter with constants. The numbers are still the same value in other representations, they are just not as easy to understand. Also, B00000000 is shown for clarity, but zero in any number format is zero (hmmm something philosophical there?)
70+
Because we are dealing with bits in a bitwise operator - it is convenient to use the binary formatter with constants. The numbers are still the same value in other representations, they are just not as easy to understand. Also, 0b00000000 is shown for clarity, but zero in any number format is zero (hmmm something philosophical there?)
7171

72-
Consequently - to clear (set to zero) bits 0 & 1 of a variable, while leaving the rest of the variable unchanged, use the compound bitwise AND operator (&=) with the constant B11111100
72+
Consequently - to clear (set to zero) bits 0 & 1 of a variable, while leaving the rest of the variable unchanged, use the compound bitwise AND operator (&=) with the constant 0b11111100
7373

7474
1 0 1 0 1 0 1 0 variable
7575
1 1 1 1 1 1 0 0 mask
@@ -93,8 +93,8 @@ So if:
9393

9494
[source,arduino]
9595
----
96-
myByte = B10101010;
97-
myByte &= B11111100; // results in B10101000
96+
myByte = 0b10101010;
97+
myByte &= 0b11111100; // results in 0b10101000
9898
----
9999

100100
[%hardbreaks]

Language/Structure/Compound Operators/compoundBitwiseOr.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,22 @@ A review of the Bitwise OR `|` operator:
4949
Bits that are "bitwise ORed" with 0 are unchanged, so if myByte is a byte variable,
5050
[source,arduino]
5151
----
52-
myByte | B00000000 = myByte;
52+
myByte | 0b00000000 = myByte;
5353
----
5454

5555
Bits that are "bitwise ORed" with 1 are set to 1 so:
5656
[source,arduino]
5757
----
58-
myByte | B11111111 = B11111111;
58+
myByte | 0b11111111 = 0b11111111;
5959
----
6060
[%hardbreaks]
6161

6262
[float]
6363
=== Notes and Warnings
64-
Because we are dealing with bits in a bitwise operator - it is convenient to use the binary formatter with constants. The numbers are still the same value in other representations, they are just not as easy to understand. Also, B00000000 is shown for clarity, but zero in any number format is zero.
64+
Because we are dealing with bits in a bitwise operator - it is convenient to use the binary formatter with constants. The numbers are still the same value in other representations, they are just not as easy to understand. Also, 0b00000000 is shown for clarity, but zero in any number format is zero.
6565
[%hardbreaks]
6666

67-
Consequently - to set bits 0 & 1 of a variable, while leaving the rest of the variable unchanged, use the compound bitwise OR operator (|=) with the constant B00000011
67+
Consequently - to set bits 0 & 1 of a variable, while leaving the rest of the variable unchanged, use the compound bitwise OR operator (|=) with the constant 0b00000011
6868

6969
1 0 1 0 1 0 1 0 variable
7070
0 0 0 0 0 0 1 1 mask
@@ -88,8 +88,8 @@ Here is the same representation with the variables bits replaced with the symbol
8888
So if:
8989
[source,arduino]
9090
----
91-
myByte = B10101010;
92-
myByte |= B00000011 == B10101011;
91+
myByte = 0b10101010;
92+
myByte |= 0b00000011 == 0b10101011;
9393
----
9494

9595
--

Language/Structure/Compound Operators/compoundBitwiseXor.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,22 @@ A review of the Bitwise XOR `^` operator:
4949
Bits that are "bitwise XORed" with 0 are left unchanged. So if myByte is a byte variable,
5050
[source,arduino]
5151
----
52-
myByte ^ B00000000 = myByte;
52+
myByte ^ 0b00000000 = myByte;
5353
----
5454

5555
Bits that are "bitwise XORed" with 1 are toggled so:
5656
[source,arduino]
5757
----
58-
myByte ^ B11111111 = ~myByte;
58+
myByte ^ 0b11111111 = ~myByte;
5959
----
6060
[%hardbreaks]
6161

6262
[float]
6363
=== Notes and Warnings
64-
Because we are dealing with bits in a bitwise operator - it is convenient to use the binary formatter with constants. The numbers are still the same value in other representations, they are just not as easy to understand. Also, B00000000 is shown for clarity, but zero in any number format is zero.
64+
Because we are dealing with bits in a bitwise operator - it is convenient to use the binary formatter with constants. The numbers are still the same value in other representations, they are just not as easy to understand. Also, 0b00000000 is shown for clarity, but zero in any number format is zero.
6565
[%hardbreaks]
6666

67-
Consequently - to toggle bits 0 & 1 of a variable, while leaving the rest of the variable unchanged, use the compound bitwise XOR operator (^=) with the constant B00000011
67+
Consequently - to toggle bits 0 & 1 of a variable, while leaving the rest of the variable unchanged, use the compound bitwise XOR operator (^=) with the constant 0b00000011
6868

6969
1 0 1 0 1 0 1 0 variable
7070
0 0 0 0 0 0 1 1 mask
@@ -88,8 +88,8 @@ Here is the same representation with the variables bits replaced with the symbol
8888
So if:
8989
[source,arduino]
9090
----
91-
myByte = B10101010;
92-
myByte ^= B00000011 == B10101001;
91+
myByte = 0b10101010;
92+
myByte ^= 0b00000011 == 0b10101001;
9393
----
9494

9595
--

Language/Variables/Constants/integerConstants.adoc

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ Normally, integer constants are treated as base 10 (decimal) integers, but speci
3232
|
3333

3434
|2 (binary)
35-
|B1111011
36-
|leading 'B'
37-
|only works with 8 bit values (0 to 255) characters 0&1 valid
35+
|0b1111011
36+
|leading "0b"
37+
|characters 0&1 valid
3838

3939
|8 (octal)
4040
|0173
@@ -76,13 +76,7 @@ Only the characters 0 and 1 are valid.
7676
=== Example Code:
7777
[source,arduino]
7878
----
79-
n = B101; // same as 5 decimal ((1 * 2^2) + (0 * 2^1) + 1)
80-
----
81-
82-
The binary formatter only works on bytes (8 bits) between 0 (B0) and 255 (B11111111). If it is convenient to input an int (16 bits) in binary form you can do it a two-step procedure such as:
83-
[source,arduino]
84-
----
85-
myInt = (B11001100 * 256) + B10101010; // B11001100 is the high byte`
79+
n = 0b101; // same as 5 decimal ((1 * 2^2) + (0 * 2^1) + 1)
8680
----
8781
[%hardbreaks]
8882

0 commit comments

Comments
 (0)