Swift Bitwise Operators
Working with individual bits in Swift
🔢 What are Bitwise Operators?
Bitwise operators in Swift manipulate individual bits within integers. They perform operations like AND, OR, XOR, and bit shifting on binary representations of numbers, useful for low-level programming and optimization tasks.
// Working with bits
let a = 5 // Binary: 101
let b = 3 // Binary: 011
let result = a & b // AND operation
Types of Bitwise Operators
Bitwise AND
Returns 1 if both bits are 1
5 & 3 // Result: 1
// 101 & 011 = 001
Bitwise OR
Returns 1 if either bit is 1
5 | 3 // Result: 7
// 101 | 011 = 111
Bitwise XOR
Returns 1 if bits are different
5 ^ 3 // Result: 6
// 101 ^ 011 = 110
Bitwise NOT
Inverts all bits (1s become 0s)
~5 // Inverts all bits
// ~101 = ...11111010
🔹 Bitwise AND (&)
The AND operator returns 1 only when both corresponding bits are 1:
let a = 12 // Binary: 1100
let b = 10 // Binary: 1010
let result = a & b // Binary: 1000 = 8
print("12 & 10 = \(result)") // Output: 12 & 10 = 8
// Practical use: Check if number is even
let number = 7
let isEven = (number & 1) == 0
print("\(number) is even: \(isEven)") // Output: 7 is even: false
Output:
7 is even: false
🔹 Bitwise OR (|)
The OR operator returns 1 when at least one corresponding bit is 1:
let x = 5 // Binary: 0101
let y = 3 // Binary: 0011
let result = x | y // Binary: 0111 = 7
print("5 | 3 = \(result)") // Output: 5 | 3 = 7
// Practical use: Setting flags
var permissions = 4 // Read permission (100)
permissions |= 2 // Add write permission (010)
permissions |= 1 // Add execute permission (001)
print("Final permissions: \(permissions)") // Output: Final permissions: 7
Output:
Final permissions: 7
🔹 Bitwise XOR (^)
The XOR operator returns 1 when bits are different:
let p = 6 // Binary: 0110
let q = 4 // Binary: 0100
let result = p ^ q // Binary: 0010 = 2
print("6 ^ 4 = \(result)") // Output: 6 ^ 4 = 2
// Practical use: Simple encryption/decryption
let message = 65 // ASCII 'A'
let key = 42
let encrypted = message ^ key
let decrypted = encrypted ^ key
print("Original: \(message), Encrypted: \(encrypted), Decrypted: \(decrypted)")
Output:
Original: 65, Encrypted: 107, Decrypted: 65
🔹 Bitwise NOT (~)
The NOT operator inverts all bits in a number:
let value: UInt8 = 5 // Binary: 00000101
let inverted = ~value // Binary: 11111010 = 250
print("~5 = \(inverted)") // Output: ~5 = 250
// With signed integers, result is -(n+1)
let signedValue = 5
let signedInverted = ~signedValue
print("~5 (signed) = \(signedInverted)") // Output: ~5 (signed) = -6
Output:
~5 (signed) = -6
🔹 Bit Shifting Operators
Shift operators move bits left or right:
🔸 Left Shift (<<)
let num = 5 // Binary: 101
let shifted = num << 2 // Binary: 10100 = 20
print("5 << 2 = \(shifted)") // Output: 5 << 2 = 20
// Left shift by n is equivalent to multiplying by 2^n
Output:
🔸 Right Shift (>>)
let num2 = 20 // Binary: 10100
let shifted2 = num2 >> 2 // Binary: 101 = 5
print("20 >> 2 = \(shifted2)") // Output: 20 >> 2 = 5
// Right shift by n is equivalent to dividing by 2^n