Skip to content

Commit 2f64a4f

Browse files
committed
Added a file for compound bitwise XOR operation ^=
1 parent fd7ce00 commit 2f64a4f

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
---
2+
title: "^="
3+
title_expanded: compound bitwise xor
4+
categories: [ "Structure" ]
5+
subCategories: [ "Compound Operators" ]
6+
---
7+
8+
= ^= Compound Bitwise XOR
9+
10+
11+
// OVERVIEW SECTION STARTS
12+
[#overview]
13+
--
14+
15+
[float]
16+
=== Description
17+
The compound bitwise XOR operator `^=` is often used with a variable and a constant to toggle (invert) particular bits in a variable.
18+
[%hardbreaks]
19+
20+
A review of the Bitwise XOR `^` operator:
21+
22+
0 0 1 1 operand1
23+
0 1 0 1 operand2
24+
----------
25+
0 1 1 0 (operand1 ^ operand2) - returned result
26+
[%hardbreaks]
27+
28+
[float]
29+
=== Syntax
30+
[source,arduino]
31+
----
32+
x ^= y; // equivalent to x = x ^ y;
33+
----
34+
35+
[float]
36+
=== Parameters
37+
`x`: variable. *Allowed data types:* char, int, long +
38+
`y`: variable or constant. *Allowed data types:* char, int, long
39+
40+
--
41+
// OVERVIEW SECTION ENDS
42+
43+
44+
45+
// HOW TO USE SECTION STARTS
46+
[#howtouse]
47+
--
48+
49+
[float]
50+
=== Example Code
51+
Bits that are "bitwise XORed" with 0 are left unchanged. So if myByte is a byte variable,
52+
[source,arduino]
53+
----
54+
myByte ^ B00000000 = myByte;
55+
----
56+
57+
Bits that are "bitwise XORed" with 1 are toggled so:
58+
[source,arduino]
59+
----
60+
myByte ^ B11111111 = ~myByte;
61+
----
62+
[%hardbreaks]
63+
64+
[float]
65+
=== Notes and Warnings
66+
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.
67+
[%hardbreaks]
68+
69+
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
70+
71+
1 0 1 0 1 0 1 0 variable
72+
0 0 0 0 0 0 1 1 mask
73+
----------------------
74+
1 0 1 0 1 0 0 1
75+
76+
bits unchanged
77+
bits toggled
78+
79+
80+
Here is the same representation with the variables bits replaced with the symbol x. ~x represents the complement of x.
81+
82+
x x x x x x x x variable
83+
0 0 0 0 0 0 1 1 mask
84+
----------------------
85+
x x x x x x ~x ~x
86+
87+
bits unchanged
88+
bits set
89+
90+
So if:
91+
[source,arduino]
92+
----
93+
myByte = B10101010;
94+
myByte ^= B00000011 == B10101001;
95+
----
96+
97+
--
98+
// HOW TO USE SECTION ENDS
99+
100+
101+
102+
103+
//SEE ALSO SECTION BEGINS
104+
[#see_also]
105+
--
106+
107+
[float]
108+
=== See also
109+
110+
[role="language"]
111+
* #LANGUAGE# link:../../bitwise-operators/bitwisexor[& Bitwise XOR]
112+
113+
--
114+
// SEE ALSO SECTION ENDS

0 commit comments

Comments
 (0)