Commutative And Associative Properties Of Bitwise Operators

Overview


Two fundamental properties of Bitwise operators are commutativity and associativity.


Let's briefly review the primary bitwise operators:




Commutative Property


The commutative property states that the order of the operands does not affect the result of the operation. For bitwise operators, this property applies to AND, OR, and XOR.



Commutativity of AND

The commutative property for the AND operator can be expressed as:

 𝐴 & 𝐵 = 𝐵 & 𝐴


Example

Consider A = 5 (binary: 0101) and B = 3 (binary: 0011).


𝐴 & 𝐵  =  0101 & 0011  =  0001  =  1

   𝐵 & 𝐴  =  0011 & 0101  =  0001  =  1


    Thus, 𝐴 & 𝐵 = 𝐵 & 𝐴



Commutativity of OR

The commutative property for the OR operator can be expressed as:

  𝐴 ∣ 𝐵 = 𝐵 ∣ 𝐴

Example

Using the same values 𝐴 = 5 and 𝐵 = 3:

                                          𝐴 ∣ 𝐵  =  0101 ∣ 0011  =  0111  =  7

                                          𝐵 ∣ 𝐴   =  0011 ∣ 0101  =  0111  = 7

            Thus, 𝐴 ∣ 𝐵 = 𝐵 ∣ 𝐴.



Commutativity of XOR

The commutative property for the XOR operator can be expressed as:

 𝐴 ^ 𝐵 = 𝐵 ^ 𝐴

Example

Using the same values 𝐴 = 5 and 𝐵 = 3 :

                                  𝐴 ^ 𝐵  =  0101 ^ 0011  =  0110  =  6

                                   𝐵 ^ 𝐴  =  0011 ^ 0101  =  0110  =  6

        Thus, 𝐴 ^ 𝐵 = 𝐵 ^ 𝐴.



Code Example

Associative Property


The associative property states that the grouping of operands does not affect the result of the operation. For bitwise operators, this property applies to AND, OR, and XOR.



Associativity of AND

The associative property for the AND operator can be expressed as: 

(𝐴 & 𝐵) & 𝐶  =  𝐴 & (𝐵 & 𝐶)

Example

Consider 𝐴 = 5 (binary: 0101), 𝐵= 3 (binary: 0011), and 𝐶 = 6 (binary: 0110).

                                (𝐴 & 𝐵) & 𝐶  =  (0101 & 0011) & 0110  =  0001 & 0110  =  0000  =  0

                                  𝐴 & (𝐵 & 𝐶)  =  0101 & (0011 & 0110)  =  0101 & 0010  =  0000  =  0


                Thus, (𝐴 & 𝐵) & 𝐶=𝐴 & (𝐵 & 𝐶).



Associativity of OR

The associative property for the OR operator can be expressed as: 

(𝐴 ∣ 𝐵) ∣ 𝐶  =  𝐴 ∣ (𝐵 ∣ 𝐶)

Example

Using the same values 𝐴  =  5, 𝐵  =  3, and 𝐶  =  6 :

                                    (𝐴 ∣ 𝐵) ∣ 𝐶  =  (0101 ∣ 0011) ∣ 0110  =  0111 ∣ 0110  =  0111  =  7

                                     𝐴 ∣ (𝐵 ∣ 𝐶)  =  0101 ∣ (0011 ∣ 0110)  =  0101 ∣ 0111  =  0111  =  7


        Thus, (𝐴 ∣ 𝐵) ∣ 𝐶  =  𝐴 ∣ (𝐵 ∣ 𝐶) .



Associativity of XOR

The associative property for the XOR operator can be expressed as: 

(𝐴 ^ 𝐵) ^ 𝐶 = 𝐴 ^ (𝐵 ^ 𝐶)

Example

Using the same values 𝐴 = 5, 𝐵 = 3, and 𝐶 = 6 :

                                (𝐴 ^ 𝐵) ^ 𝐶  =  (0101 ^ 0011) ^ 0110  =  0110 ^ 0110  =  0000  = 0

                                 𝐴 ^ (𝐵 ^ 𝐶)  =  0101 ^ (0011 ^ 0110)  =  0101 ^ 0101  =  0000  =  0


            Thus, (𝐴 ^ 𝐵) ^ 𝐶 = 𝐴 ^ (𝐵 ^ 𝐶) .



Code Example

Conclusion

The commutative and associative properties of bitwise operators are powerful tools in the arsenal of any programmer. By leveraging these properties, you can simplify complex expressions, optimize your code, and gain deeper insights into the underlying mechanics of bitwise operations.