Boolean Logic & Bitwise Operators Tutorial and Walkthrough in Swift

Full Code: Boolean Logic & Bit MAnipulation Tutorial

Boolean Logic & Bit Manipulation

A lot of confusion came from my MPU-6050 Tutorial (Link to the tutorial: http://eeenthusiast.com/arduino-accelerometer-gyroscope-tutorial-mpu-6050/). It came mainly from the bit manipulation of data. The goal of this tutorial is to clarify this operation and give a review on boolean logic. Both of these notions are very important in software & hardware development. We will be utilizing Swift’s playgrounds as the platform for the demo. Do note that these operations are present in 99% of the languages out there.

Hardware & Software for the Project

We will be using Xcode 8.0 and Swift 3.0 for this tutorial.

Boolean Logic

There are 4 crucial logic operators you must know. They are AND, OR, NOT and XOR. We will discuss each one of them in detail.

“AND” Logic Operator (&)

The AND operator will take two operands and return 1 if they are both 1. It will return 0 otherwise. This results in the following truth table:

  • (0 & 0) = 0
  • (0 & 1) = 0
  • (1 & 0) = 0
  • (1 & 1) = 1
“OR” Logic Operator (|)

The OR operator will take two operands and return 1 if either one is 1. It will return 0 otherwise. This results in the following truth table:

  • (0 | 0) = 0
  • (0 | 1) = 1
  • (1 | 0) = 1
  • (1 | 1) = 1
“XOR” Logic Operator (^)

The XOR operator will take two operands and return 1 if only one is equal to 1. It will return 0 otherwise. This results in the following truth table:

  • (0 ^ 0) = 0
  • (0 ^ 1) = 1
  • (1 ^ 0) = 1
  • (1 ^ 1) = 0
“NOT” Logic Operator(~, ! in some languages)

The NOT operator will take one operand and return 1 if it is equal to 0. It will return 0 otherwise. This results in the following truth table:

  • (~ 0) = 1
  • (~ 1) = 0

Integer Inversion

Integers are represented in binary in computers. A typical integer will be represented by 32 bits. A negative integer will be represented by 2’s compliment (Wiki Link: Two’s Compliment) of the positive integer. It is important to know how to convert between both through bit manipulation.

The simplest way to convert 67 to -67 (as an example) is to use the NOT operator and add 1. Here’s the code which does just that.
var D: Int8 = 67
var E = ~D + 1 // E = -67

To convert back to 67, we do the same operation
var F = ~E + 1 // F = 67

Bit Shift

There are two operations you can use in order to shift bits in an integer. They are “>>” and “<<". One of them will insert a 0 on the right and the other on the left. Here are two examples as to how they work. A = 0b1010 C = A << 2 // C = 101000 D = 0b1010 E = D << 4 // E = 10100000 A = 0b101000 C = A >> 2 // C = 10101

Data Combining

What I had done in the MPU-6050 tutorial, was combining two bytes of data. This operation is possible with the bit shift and the boolean logic explained above. Here is the operation:
var Data1: UInt16 = 0b10101010 // First byte of data.
var Data2: UInt16 = 0b11001100 // Second byte of data.
var Data3: UInt16 = (Data1 << 8) | Data2 Walkthrough: Data1 << 8 = 1010101000000000 (Data1 << 8) | Data2 = 1010101011001100 (The two bytes combined)

Video Tutorial:

Conclusion

Boolean Logic and Bit Shift operators are extremely important. They can allow us to easily perform data manipulation and verification. In this tutorial, we’ve reviewed boolean logic operators, bit shift operators and looked at two’s compliment of integers. We’ve also completed an example of how to combine two bytes of data.

Thank you for reading & watching,
– Vlad Romanov, EEEnthusiast

Leave a Reply

Your email address will not be published.

CommentLuv badge