---
title: FixedWidthInteger
framework: swift
role: symbol
role_heading: Protocol
path: swift/fixedwidthinteger
---

# FixedWidthInteger

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

## Declaration

```swift
protocol FixedWidthInteger : BinaryInteger, LosslessStringConvertible where Self.Magnitude : FixedWidthInteger, Self.Magnitude : UnsignedInteger, Self.Stride : FixedWidthInteger, Self.Stride : SignedInteger
```

## Overview

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. extension FixedWidthInteger {     var binaryString: String {         var result: [String] = []         for i in 0..<(Self.bitWidth / 8) {             let byte = UInt8(truncatingIfNeeded: self >> (i * 8))             let byteString = String(byte, radix: 2)             let padding = String(repeating: "0",                                  count: 8 - byteString.count)             result.append(padding + byteString)         }         return "0b" + result.reversed().joined(separator: "_")     } }

print(Int16.max.binaryString) // Prints "0b01111111_11111111" print((101 as UInt8).binaryString) // Prints "0b01100101" 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. func squared<T: FixedWidthInteger>(_ x: T) -> T? {     let (result, overflow) = x.multipliedReportingOverflow(by: x)     if overflow {         return nil     }     return result }

let (x, y): (Int8, Int8) = (9, 123) print(squared(x)) // Prints "Optional(81)" print(squared(y)) // Prints "nil" 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.

## Topics

### Operators

- [&*(_:_:)](swift/fixedwidthinteger/&*(_:_:).md)
- [&*=(_:_:)](swift/fixedwidthinteger/&*=(_:_:).md)
- [&+(_:_:)](swift/fixedwidthinteger/&+(_:_:).md)
- [&+=(_:_:)](swift/fixedwidthinteger/&+=(_:_:).md)
- [&-(_:_:)](swift/fixedwidthinteger/&-(_:_:).md)
- [&-=(_:_:)](swift/fixedwidthinteger/&-=(_:_:).md)
- [&>>(_:_:)](swift/fixedwidthinteger/&__(_:_:)-1sn91.md)
- [&<<(_:_:)](swift/fixedwidthinteger/&__(_:_:)-4j1s7.md)
- [&>>=(_:_:)](swift/fixedwidthinteger/&__=(_:_:)-2ffyd.md)
- [&<<=(_:_:)](swift/fixedwidthinteger/&__=(_:_:)-q186.md)

### Initializers

- [init(_:radix:)](swift/fixedwidthinteger/init(_:radix:).md)
- [init(bigEndian:)](swift/fixedwidthinteger/init(bigendian:).md)
- [init(littleEndian:)](swift/fixedwidthinteger/init(littleendian:).md)

### Instance Properties

- [bigEndian](swift/fixedwidthinteger/bigendian.md)
- [byteSwapped](swift/fixedwidthinteger/byteswapped.md)
- [leadingZeroBitCount](swift/fixedwidthinteger/leadingzerobitcount.md)
- [littleEndian](swift/fixedwidthinteger/littleendian.md)
- [nonzeroBitCount](swift/fixedwidthinteger/nonzerobitcount.md)

### Instance Methods

- [addingReportingOverflow(_:)](swift/fixedwidthinteger/addingreportingoverflow(_:).md)
- [dividedReportingOverflow(by:)](swift/fixedwidthinteger/dividedreportingoverflow(by:).md)
- [dividingFullWidth(_:)](swift/fixedwidthinteger/dividingfullwidth(_:).md)
- [multipliedFullWidth(by:)](swift/fixedwidthinteger/multipliedfullwidth(by:).md)
- [multipliedReportingOverflow(by:)](swift/fixedwidthinteger/multipliedreportingoverflow(by:).md)
- [remainderReportingOverflow(dividingBy:)](swift/fixedwidthinteger/remainderreportingoverflow(dividingby:).md)
- [subtractingReportingOverflow(_:)](swift/fixedwidthinteger/subtractingreportingoverflow(_:).md)

### Type Properties

- [bitWidth](swift/fixedwidthinteger/bitwidth.md)
- [max](swift/fixedwidthinteger/max.md)
- [min](swift/fixedwidthinteger/min.md)

### Type Methods

- [random(in:)](swift/fixedwidthinteger/random(in:)-3uaq4.md)
- [random(in:)](swift/fixedwidthinteger/random(in:)-467fr.md)
- [random(in:using:)](swift/fixedwidthinteger/random(in:using:)-33n1n.md)
- [random(in:using:)](swift/fixedwidthinteger/random(in:using:)-4byak.md)

## Relationships

### Inherits From

- [AdditiveArithmetic](swift/additivearithmetic.md)
- [BinaryInteger](swift/binaryinteger.md)
- [Comparable](swift/comparable.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [Equatable](swift/equatable.md)
- [ExpressibleByIntegerLiteral](swift/expressiblebyintegerliteral.md)
- [Hashable](swift/hashable.md)
- [LosslessStringConvertible](swift/losslessstringconvertible.md)
- [Numeric](swift/numeric.md)
- [Strideable](swift/strideable.md)

### Conforming Types

- [Int](swift/int.md)
- [Int128](swift/int128.md)
- [Int16](swift/int16.md)
- [Int32](swift/int32.md)
- [Int64](swift/int64.md)
- [Int8](swift/int8.md)
- [UInt](swift/uint.md)
- [UInt128](swift/uint128.md)
- [UInt16](swift/uint16.md)
- [UInt32](swift/uint32.md)
- [UInt64](swift/uint64.md)
- [UInt8](swift/uint8.md)

## See Also

### Integer

- [BinaryInteger](swift/binaryinteger.md)
- [SignedInteger](swift/signedinteger.md)
- [UnsignedInteger](swift/unsignedinteger.md)
