Enumeration
OctetArrangement
public enum OctetArrangement
The way in which octets are arranged to form a multi-byte integer.
Applications should prefer to work with individual octets wherever possible, as octets have a consistent numeric interpretation and binary representation across machines of different endianness.
When combining octets in to larger (e.g. 16- or 32-bit) integers, we have to consider that machines have a choice in how their octets are arranged in memory.
For instance, the first piece of the IPv6 address 2608::3:5
consists of 2 octets, 0x26
and 0x08
; if we created a 16-bit integer with those same octets
arranged in that order, a big-endian machine would read this as having numeric value 9736 (for the purposes of integer-level operations, such as addition),
whereas a little-endian machine would consider the same octets to contain the numeric value 2086.
Hence there are 2 ways to combine octets in to larger integers:
-
With the same octets in the same order in memory. We call this the
binary
interpretation, although it is more-commonly known as "network" byte order, or big-endian. As noted above, integers derived from the same address using thebinary
interpretation may have different numeric values on different machines. -
By rearranging octets to give a consistent numeric value. We call this the
numeric
interpretation, although it is more-comonly known as "host" byte-order. For instance, when reading the first 16-bit piece of the above address on a little-endian machine, the octets0x26 0x08
will be reordered to0x08 0x26
, so that the numeric value (9736) is the same as the hexadecimal number0x2608
. Assigning a group of octets using thenumeric
integer 9736 will similarly reorder the octets, so that they appear as the octet sequence0x26 0x08
in the address.
Enumeration Cases
numeric
case numeric
Offers consistent numeric values across machines of different endianness, by adjusting the binary representation when reading or writing multi-byte integers. Also known as host byte order (i.e. the integers that you read and write are expected to be in host byte order).
binary
case binary
Offers consistent binary representations across machines of different endianness, although each machine may interpret those bits as a different numeric value. Also known as network byte order (i.e. the integers that you read and write are expected to be in network byte order).
Properties
hostOrder
@inlinable public static var hostOrder: Self
A synonym for .numeric
.
networkOrder
@inlinable public static var networkOrder: Self
A synonym for .binary
.