protocol
BitwiseOperations
Inheritance | View Protocol Hierarchy → |
---|---|
Import |
|
Static Variables
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
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:
Performing a bitwise AND operation with a value and allZeros
always
returns allZeros
.
(
x
&
.
allZeros
)
// 0b00000000
// Prints "0"
Complexity: O(1).
Declaration
func
&
(
lhs
:
Self
,
rhs
:
Self
) -
>
Self
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:
Performing a bitwise XOR with a value and allZeros
always returns the
same value:
(
x
^ .
allZeros
)
// 0b00000101
// Prints "5"
Complexity: O(1).
Declaration
func
^(
lhs
:
Self
,
rhs
:
Self
) -
>
Self
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:
Performing a bitwise OR operation with a value and allZeros
always
returns the same value.
(
x
| .
allZeros
)
// 0b00000101
// Prints "5"
Complexity: O(1).
Declaration
func
|(
lhs
:
Self
,
rhs
:
Self
) -
>
Self
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
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 toBitwiseOperations
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:
Here are the required operators for the
BitwiseOperations
protocol:The bitwise OR operator (
|
) returns a value that has each bit set to1
where one or both of its arguments had that bit set to1
. This is equivalent to the union of two sets. For example:Performing a bitwise OR operation with a value and
allZeros
always returns the same value.The bitwise AND operator (
&
) returns a value that has each bit set to1
where both of its arguments had that bit set to1
. This is equivalent to the intersection of two sets. For example:Performing a bitwise AND operation with a value and
allZeros
always returnsallZeros
.The bitwise XOR operator (
^
), or exclusive OR operator, returns a value that has each bit set to1
where one or the other but not both of its operators has that bit set to1
. This is equivalent to the symmetric difference of two sets. For example:Performing a bitwise XOR operation with a value and
allZeros
always returns the same value.The bitwise NOT operator (
~
) is a prefix operator that returns a value where all the bits of its argument are flipped: Bits that are1
in the argument are0
in the result, and bits that are0
in the argument are1
in the result. This is equivalent to the inverse of a set. For example:Performing a bitwise NOT operation on
allZeros
returns a value with every bit set to1
.The
OptionSet
protocol uses a raw value that conforms toBitwiseOperations
to provide mathematical set operations likeunion(_:)
,intersection(_:)
andcontains(_:)
with O(1) performance.Conforming to the BitwiseOperations Protocol
To make your custom type conform to
BitwiseOperations
, add a staticallZeros
property and declare the four required operator functions. Any type that conforms toBitwiseOperations
, wherex
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