An integer type that uses a fixed size for every instance.

SDK

Xcode 9.0+

Framework

Swift Standard Library

Declaration

Overview

The FixedWidthInteger protocol adds binary bitwise operations, bit shifts, and overflow handling to the operations supported by the BinaryInteger protocol.

Use the FixedWidthInteger protocol as a constraint or extension point when writing operations that depend on bit shifting, performing bitwise operations, catching overflows, or having access to the maximum or minimum representable value of a type. For example, the following code provides a binaryString property on every fixed-width integer that represents the number’s binary representation, split into 8-bit chunks.

The binaryString implementation uses the static bitWidth property and the right shift operator (>>), both of which are available to any type that conforms to the FixedWidthInteger protocol.

The next example declares a generic squared function, which accepts an instance x of any fixed-width integer type. The function uses the multipliedReportingOverflow(by:) method to multiply x by itself and check whether the result is too large to represent in the same type.

Conforming to the FixedWidthInteger Protocol

To make your own custom type conform to the FixedWidthInteger protocol, declare the required initializers, properties, and methods. The required methods that are suffixed with ReportingOverflow serve as the customization points for arithmetic operations. When you provide just those methods, the standard library provides default implementations for all other arithmetic methods and operators.

Returns the quotient obtained by dividing this value by the given value, along with a Boolean value indicating whether overflow occurred in the operation.

Returns the difference obtained by subtracting the given value from this value, along with a Boolean value indicating whether overflow occurred in the operation.

Returns the result of shifting a value’s binary representation the specified number of digits to the left, masking the shift amount to the type’s bit width.

Returns the result of shifting a value’s binary representation the specified number of digits to the left, masking the shift amount to the type’s bit width, and stores the result in the left-hand-side variable.

Returns the result of shifting a value’s binary representation the specified number of digits to the right, masking the shift amount to the type’s bit width.

Calculates the result of shifting a value’s binary representation the specified number of digits to the right, masking the shift amount to the type’s bit width, and stores the result in the left-hand-side variable.

An integer type that can represent only nonnegative values.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.