Skip to main content

Bitwise and Ternary Conditional Operators

Bitwise operators

Bitwise operators enable the manipulation of individual bits of unsigned and signed integers. They're often used in low-level programming.

  • Bitwise AND: a & b

    Returns a new integer whose bits are 1 only if the bits were 1 in both input integers:


    _10
    let firstFiveBits = 0b11111000
    _10
    let lastFiveBits = 0b00011111
    _10
    let middleTwoBits = firstFiveBits & lastFiveBits // is 0b00011000

  • Bitwise OR: a | b

    Returns a new integer whose bits are 1 only if the bits were 1 in either input integers:


    _10
    let someBits = 0b10110010
    _10
    let moreBits = 0b01011110
    _10
    let combinedbits = someBits | moreBits // is 0b11111110

  • Bitwise XOR: a ^ b

    Returns a new integer whose bits are 1 where the input bits are different, and are 0 where the input bits are the same:


    _10
    let firstBits = 0b00010100
    _10
    let otherBits = 0b00000101
    _10
    let outputBits = firstBits ^ otherBits // is 0b00010001

Bitwise shifting operators

  • Bitwise LEFT SHIFT: a << b

    Returns a new integer with all bits moved to the left by a certain number of places:


    _10
    let someBits = 4 // is 0b00000100
    _10
    let shiftedBits = someBits << 2 // is 0b00010000

  • Bitwise RIGHT SHIFT: a >> b

    Returns a new integer with all bits moved to the right by a certain number of places:


    _10
    let someBits = 8 // is 0b00001000
    _10
    let shiftedBits = someBits >> 2 // is 0b00000010

For unsigned integers, the bitwise shifting operators perform logical shifting; for signed integers, they perform arithmetic shifting. Also note that for a << b or a >> b, b must fit into a 64-bit integer.

Ternary conditional operator

There is only one ternary conditional operator (e.g., a ? b : c).

It behaves like an if-statement, but is an expression: if the first operator value is true, the second operator value is returned. If the first operator value is false, the third value is returned.

The first value must be a boolean, or resolve to one (and must have the type Bool). The second value and third value can be of any type. The result type is the least common supertype of the second and third value.


_10
let x = 1 > 2 ? 3 : 4
_10
// `x` is `4` and has type `Int`
_10
_10
let y = 1 > 2 ? nil : 3
_10
// `y` is `3` and has type `Int?`