Swift Bitwise Operators

In Swift, bitwise operators are used to manipulate individual bits within integers. These operators are essential for tasks such as low-level programming, optimizing performance, and working with binary data. Bitwise operations allow you to perform AND, OR, XOR, and other operations directly on the binary representation of numbers.


Bitwise Operators in Swift

Here is a list of bitwise operators available in Swift:

OperatorDescriptionExample
&Bitwise ANDSets each bit to 1 if both bits are 1
|Bitwise ORSets each bit to 1 if one of the bits is 1
^Bitwise XORSets each bit to 1 if only one of the bits is 1
~Bitwise NOTInverts all the bits
<<Left ShiftShifts bits to the left by a specified number of positions
>>Right ShiftShifts bits to the right by a specified number of positions
Bitwise Operators in Swift

Example: Using Bitwise Operators

This example demonstrates the basic use of bitwise operators:

File: main.swift

</>
Copy
let a: UInt8 = 0b10101010
let b: UInt8 = 0b11001100

// Bitwise AND
let andResult = a & b
print("AND result: \(String(andResult, radix: 2))")

// Bitwise OR
let orResult = a | b
print("OR result: \(String(orResult, radix: 2))")

// Bitwise XOR
let xorResult = a ^ b
print("XOR result: \(String(xorResult, radix: 2))")

// Bitwise NOT
let notResult = ~a
print("NOT result: \(String(notResult, radix: 2))")

Explanation:

  • a and b are binary numbers represented in an 8-bit format.
  • The & operator performs a bitwise AND operation, keeping bits that are 1 in both a and b.
  • The | operator performs a bitwise OR operation, keeping bits that are 1 in either a or b.
  • The ^ operator performs a bitwise XOR operation, keeping bits that are 1 in either a or b but not both.
  • The ~ operator inverts all bits in a.

Output:

Example for Using Bitwise Operators in Swift

Example 1: Left and Right Shift

This example demonstrates bitwise shifting: both left and right. We take an unsigned integer with eight bits, and shift if left by 2 bits, and the shift the same value right by 2 bits, and print the outputs.

File: main.swift

</>
Copy
let value: UInt8 = 0b00001111

// Left shift
let leftShift = value << 2
print("Left shift result: \(String(leftShift, radix: 2))")

// Right shift
let rightShift = value >> 2
print("Right shift result: \(String(rightShift, radix: 2))")

Explanation:

  • The << operator shifts the bits of value to the left by 2 positions, adding zeros on the right.
  • The >> operator shifts the bits of value to the right by 2 positions, discarding bits on the right.

Output:

Example for using Left and Right Shift Operators in Swift

Example 2: Masking and Filtering Bits using Bitwise Operators

This example shows how to use bitwise operators for masking:

File: main.swift

</>
Copy
let value: UInt8 = 0b11010101
let mask: UInt8 = 0b00001111

// Apply mask
let maskedValue = value & mask
print("Masked result: \(String(maskedValue, radix: 2))")

Explanation:

  • The mask 0b00001111 isolates the last 4 bits of the value.
  • The & operator ensures only the last 4 bits of value remain.

Output:

Example for Masking and Filtering Bits using Bitwise Operators in Swift

Conclusion

Bitwise operators are powerful tools for working directly with binary data in Swift. They are commonly used in performance-critical applications, graphics programming, and low-level hardware interactions.