BitwiseOperations

protocol BitwiseOperations

A type that supports standard bitwise arithmetic operators.

Types that conform to the BitwiseOperations protocol implement operators for bitwise arithmetic. The integer types in the standard library all conform to BitwiseOperations by default. When you use bitwise operators with an integer, you perform operations on the raw data bits that store the integer's value.

In the following examples, the binary representation of any values are shown in a comment to the right, like this:

let x: UInt8 = 5        // 0b00000101

Here are the required operators for the BitwiseOperations protocol:

  • The bitwise OR operator (|) returns a value that has each bit set to 1 where one or both of its arguments had that bit set to 1. This is equivalent to the union of two sets. For example:

    let x: UInt8 = 5        // 0b00000101
    let y: UInt8 = 14       // 0b00001110
    let z = x | y           // 0b00001111

    Performing a bitwise OR operation with a value and allZeros always returns the same value.

    print(x | .allZeros)    // 0b00000101
    // Prints "5"
  • The bitwise AND operator (&) returns a value that has each bit set to 1 where both of its arguments had that bit set to 1. This is equivalent to the intersection of two sets. For example:

    let x: UInt8 = 5        // 0b00000101
    let y: UInt8 = 14       // 0b00001110
    let z = x & y           // 0b00000100

    Performing a bitwise AND operation with a value and allZeros always returns allZeros.

    print(x & .allZeros)    // 0b00000000
    // Prints "0"
  • The bitwise XOR operator (^), or exclusive OR operator, returns a value that has each bit set to 1 where one or the other but not both of its operators has that bit set to 1. This is equivalent to the symmetric difference of two sets. For example:

    let x: UInt8 = 5        // 0b00000101
    let y: UInt8 = 14       // 0b00001110
    let z = x ^ y           // 0b00001011

    Performing a bitwise XOR operation with a value and allZeros always returns the same value.

    print(x ^ .allZeros)    // 0b00000101
    // Prints "5"
  • The bitwise NOT operator (~) is a prefix operator that returns a value where all the bits of its argument are flipped: Bits that are 1 in the argument are 0 in the result, and bits that are 0 in the argument are 1 in the result. This is equivalent to the inverse of a set. For example:

    let x: UInt8 = 5        // 0b00000101
    let notX = ~x           // 0b11111010

    Performing a bitwise NOT operation on allZeros returns a value with every bit set to 1.

    let allOnes = ~UInt8.allZeros   // 0b11111111

The OptionSet protocol uses a raw value that conforms to BitwiseOperations to provide mathematical set operations like union(_:), intersection(_:) and contains(_:) with O(1) performance.

Conforming to the BitwiseOperations Protocol

To make your custom type conform to BitwiseOperations, add a static allZeros property and declare the four required operator functions. Any type that conforms to BitwiseOperations, where x is an instance of the conforming type, must satisfy the following conditions:

  • x | Self.allZeros == x
  • x ^ Self.allZeros == x
  • x & Self.allZeros == .allZeros
  • x & ~Self.allZeros == x
  • ~x == x ^ ~Self.allZeros

See Also: OptionSet

Inheritance View Protocol Hierarchy →
Import import Swift

Static Variables

static var allZeros: Self Required

The empty bitset.

The allZeros static property is the identity element for bitwise OR and XOR operations and the fixed point for bitwise AND operations. For example:

let x: UInt8 = 5        // 0b00000101

// Identity
x | .allZeros           // 0b00000101
x ^ .allZeros           // 0b00000101

// Fixed point
x & .allZeros           // 0b00000000

Declaration

static var allZeros: Self { get }

Instance Methods

func &(_:rhs:) Required

Returns the intersection of bits set in the two arguments.

The bitwise AND operator (&) returns a value that has each bit set to 1 where both of its arguments had that bit set to 1. This is equivalent to the intersection of two sets. For example:

let x: UInt8 = 5        // 0b00000101
let y: UInt8 = 14       // 0b00001110
let z = x & y           // 0b00000100

Performing a bitwise AND operation with a value and allZeros always returns allZeros.

print(x & .allZeros)    // 0b00000000
// Prints "0"

Complexity: O(1).

Declaration

func &(lhs: Self, rhs: Self) -> Self
func ^(_:rhs:) Required

Returns the bits that are set in exactly one of the two arguments.

The bitwise XOR operator (^), or exclusive OR operator, returns a value that has each bit set to 1 where one or the other but not both of its operators has that bit set to 1. This is equivalent to the symmetric difference of two sets. For example:

let x: UInt8 = 5        // 0b00000101
let y: UInt8 = 14       // 0b00001110
let z = x ^ y           // 0b00001011

Performing a bitwise XOR with a value and allZeros always returns the same value:

print(x ^ .allZeros)    // 0b00000101
// Prints "5"

Complexity: O(1).

Declaration

func ^(lhs: Self, rhs: Self) -> Self
func |(_:rhs:) Required

Returns the union of bits set in the two arguments.

The bitwise OR operator (|) returns a value that has each bit set to 1 where one or both of its arguments had that bit set to 1. For example:

let x: UInt8 = 5        // 0b00000101
let y: UInt8 = 14       // 0b00001110
let z = x | y           // 0b00001111

Performing a bitwise OR operation with a value and allZeros always returns the same value.

print(x | .allZeros)    // 0b00000101
// Prints "5"

Complexity: O(1).

Declaration

func |(lhs: Self, rhs: Self) -> Self
prefix func ~(_:) Required

Returns the inverse of the bits set in the argument.

The bitwise NOT operator (~) is a prefix operator that returns a value in which all the bits of its argument are flipped: Bits that are 1 in the argument are 0 in the result, and bits that are 0 in the argument are 1 in the result. This is equivalent to the inverse of a set. For example:

let x: UInt8 = 5        // 0b00000101
let notX = ~x           // 0b11111010

Performing a bitwise NOT operation on allZeros returns a value with every bit set to 1.

let allOnes = ~UInt8.allZeros   // 0b11111111

Complexity: O(1).

Declaration

prefix func ~(x: Self) -> Self