Skip to content

Commit 1cb2d98

Browse files
authored
Merge pull request "Note architecture and board specificity of bitwise operators example code" #489 from per1234
Note architecture and board specificity of bitwise operators example code
2 parents 903a1bb + 2118c8a commit 1cb2d98

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

Language/Structure/Bitwise Operators/bitwiseAnd.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ int c = a & b; // result: 0000000001000100, or 68 in decimal.
5454
Each of the 16 bits in a and b are processed by using the bitwise AND, and all 16 resulting bits are stored in c, resulting in the value 01000100 in binary, which is 68 in decimal.
5555
[%hardbreaks]
5656

57-
One of the most common uses of bitwise AND is to select a particular bit (or bits) from an integer value, often called masking. See below for an example
57+
One of the most common uses of bitwise AND is to select a particular bit (or bits) from an integer value, often called masking. See below for an example (AVR architecture specific).
5858

5959
[source,arduino]
6060
----
61-
PORTD = PORTD & B00000011; // clear out bits 2 - 7, leave pins 0 and 1 untouched (xx & 11 == xx)
61+
PORTD = PORTD & B00000011; // 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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ One of the most common uses of the Bitwise OR is to set multiple bits in a bit-p
5353

5454
[source,arduino]
5555
----
56-
DDRD = DDRD | B11111100; // set direction bits for pins 2 to 7, leave 0 and 1 untouched (xx | 00 == xx)
57-
// same as pinMode(pin, OUTPUT) for pins 2 to 7
56+
// Note: This code is AVR architecture specific
57+
// set direction bits for pins 2 to 7, leave PD0 and PD1 untouched (xx | 00 == xx)
58+
// same as pinMode(pin, OUTPUT) for pins 2 to 7 on Uno or Nano
59+
DDRD = DDRD | B11111100;
5860
----
5961

6062
--

Language/Structure/Bitwise Operators/bitwiseXor.adoc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,19 @@ int z = x ^ y; // binary: 0110, or decimal 6
4949
----
5050
[%hardbreaks]
5151

52-
The ^ operator is often used to toggle (i.e. change from 0 to 1, or 1 to 0) some of the bits in an integer expression. In a bitwise XOR operation if there is a 1 in the mask bit, that bit is inverted; if there is a 0, the bit is not inverted and stays the same. Below is a program to blink digital pin 5.
52+
The ^ operator is often used to toggle (i.e. change from 0 to 1, or 1 to 0) some of the bits in an integer expression. In a bitwise XOR operation if there is a 1 in the mask bit, that bit is inverted; if there is a 0, the bit is not inverted and stays the same.
5353

5454
[source,arduino]
5555
----
56-
// Blink_Pin_5
57-
// demo for Exclusive OR
56+
// Note: This code uses registers specific to AVR microcontrollers (Uno, Nano, Leonardo, Mega, etc.)
57+
// it will not compile for other architectures
5858
void setup(){
59-
DDRD = DDRD | B00100000; // set digital pin five as OUTPUT
59+
DDRB = DDRB | B00100000; // 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-
PORTD = PORTD ^ B00100000; // invert bit 5 (digital pin 5), leave others untouched
64+
PORTB = PORTB ^ B00100000; // invert PB5, leave others untouched
6565
delay(100);
6666
}
6767
----

0 commit comments

Comments
 (0)