Swift: Bytes for Beginners (Part IV)

In the documentation, Apple explains the numbers that a byte array generated from a utf8, utf16 or unicode scalar would contain. It also, in the advanced section, explains that we can directly input binary numbers.

In my view, this is no small step for those unacquainted with byte arrays and binary numbers, so this post aims to provide a small bridge between these two parts of the documentation as a helping hand to anyone who needs it.

8-bit binary number

We enter a binary number into Xcode using the 0b prefix. So try this now. Write a 0b with any number of zeroes and ones after it in a playground file.
0b00000000000000000000000010101010 // returns 170

Next delete any number of zeroes after the "b" but before the first 1.
0b10101010 // returns 170

The number remains the same, those zeroes turned out to be unnecessary padding, but delete a one or change it to zero and your result changes.

Now let's try assigning a binary number to a UInt8:
let int:UInt8 = 0b00000000000000000000000010101010 // error

The compiler issues a warning. Not because a UInt8 cannot accept a binary number but because it can only accept a binary number up to a maximum of 8-bits: a string of a combination of eight zeroes or ones. So let's try again:
let int:UInt8 = 0b10101010 // returns 170

Great. So we now know that when a UInt8 byte array contains the number 170 its binary equivalent is 10101010. The question is how do we know what the decimal equivalent of a binary number will be and why is this important.

Calculating the decimal value from a binary

The importance of knowing how a binary relates to a decimal will become clear when we move on to advanced operators in a later post, for now let's handle the task of translating a binary number into a decimal one.

Each position in a binary, starting at the far right, has a value that starts at one and doubles with each move to the left. To calculate the total (decimal value) you add the value of each "on" binary position. This is best illustrated in the interactive example below.

0b 0 0 0 0 0 0 0 0 Val
0 0 0 0 0 0 0 0 0

Tapping each blue square changes the zero to a one and we see the decimal equivalent of the binary string calculated in the lower row.

With all the ones turned on, we have the maximum value of a UInt8, which is 255. (And the minimum with all turned off is 0.)

To test the truth of these findings, try assigning decimal values above 255 or below 0 to a UInt8 in your playground file.
let int:UInt8 = 256 // error
let int:UInt8 = -1 // error

You will receive an error from the compiler, because the numbers are outside the boundaries of an unsigned 8-bit number.

Assigning a binary number to a UInt16

A UInt8 is capable of handling a binary number containing a maximum of eight zeroes and ones, whereas a UInt16 is a 16-bit number and so our binary string can go up to 16 zeroes and ones, each one of the binary digits representing the following numbers from left to right:

32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1

The value (as with our shorter 8-bit number) doubles each time from right to left, and we might utilise this by writing:
let int:UInt16 = 0b01011010101010101 // returns 46,421

As before each one of those binary zeroes and ones represents a bit and a bit represents a decimal number for our UInt.

Maximum values

As we go from UInt16 to UInt32 and then on to UInt64 the number of binary digits doubles each time and so the maximum number a UInt can accept increases. To discover the maximum value of any UInt (i.e., what happens when we supply a maximum length binary string of all ones) we have the maximum value constants:
UINT8_MAX  // 255
UINT16_MAX // 65,535
UINT32_MAX // 4,294,967,295
UINT64_MAX // 18446744073709551615

Before we continue

If you've read this post and come away with an understanding of how binary values work and the limits of a UInt to a specified number of bits, then you are in a fantastic position to continue. If you didn't quite get it, then read the post again or search around the Internet for some different explanations.

Comments