Make App Pie

Training for Developers and Artists

Tips: Why use UInt8

While you usually use an integer for counting, indexing and some math, there’s also its pure binary form the unsigned integer UInt. Let’s look at what you can do with an unsigned integer.
Open up a blank swift playground named Unsigned Integer. Delete what’s there. Declare a variable as a UInt8 with the value 1.

var a:UInt8 = 1 //0000 0001

This is an eight bit binary number. By getting rid of the sign and making everything positive,  you access all the bits for data manipulation. It translates to binary as 0000 0001. This translates easily into a two digit hexadecimal number which is easier to use for unsigned integers.

If you are not familiar with hex numbers, they are base sixteen numbers. One digit equals four binary bits. This makes them a great shorthand for long binary numbers. The digit is the decimal equivalent of 0 -9, and then uses letters. Ten equals A 11, is B, 12 is C, 13 D 14 is E, and 15 is F.


Decimal Hexadecimal Binary
0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
8 8 1000
9 9 1001
10 A 1010
11 B 1011
12 C 1100
13 D 1101
14 E 1110
15 F 1111

You can assign a hex number by prefixing the number with 0x. I’ll assign b to the hex value 0xAA.

var b:UInt8 = 0xAA //1010 1010

This is 1010 1010. Unless the value is explicitly a UInt, implicit typing will guess wrong, so always include UInt8 in your declaration. One exception is an initializer that takes strings for example.

var c = UInt8("F6",radix:16)!

Don’t forget the exclamation mark. This returns  nil  if the string is improperly formed. If you were using this in code, check for the nil.
I can also print it out using a string function

print (String(a,radix:2))

Will print a string in binary of the number.I’ll copy that and print b and c too.

print (String(b,radix:2))
print (String(c,radix:2))

The console reads

Sadly it does not put leading zeroes in.

You don’t normally use arithmetic operators on these, but bit logic operators. These are AND, OR, XOR, and NOT.
Set a to 3 above the print statements.

a = 0x3 //0000 0011

Then add this code:

c = a & b

Any bit that is one in both a and b is 1 in c. Otherwise it is zero. This is the AND operator. The OR operator turns to one any bit that is one in either a or b. Try that:

c = a | b

Exclusive or(XOR) is like OR, but in cases where the bit is the same in both, its zero. So the second bit in both is on and becomes 0. That’s a tilde.

c = a ^ b

You can also invert all the zeros to ones and ones to zeroes. That is NOT.

c = ~a

There’s also bit shift left and right. I can shift a to the left two bits like this:

a = a << 2

The ones shift to the left two bits and their place is filled with zeros. If they shift outside the number they drop off. For example a would become zero if I shift right.
You’ll often be shifting left, and that’s for the most common use of bitwise operations. We compare one number of data with another that’s has a mask asking what situations are true.
If I wanted to find out if the second bit of B was on, I’d use a combination of shift and the AND operator. Comment out the previous code and add this:

a = a << 1
c = b & a
if c != 0 {print("True")}

I can also check more than one at once. Add these two lines before c:

var d:UInt8 = 1 << 3 
a = a | d 

I added a check of the fourth bit. I made a bit in the fourth place and then using or turned it on in a. That’s true If I shift the bit mask one to the left,

 c = b & (a >> 1)

its false.

You can do this for each bit you want to test. Often you’ll have a set of constants ready for the tests you want to do.
You’ll find bit-level comparisons used in hardware related code, graphics and some will use it for wireless data transmission. While fast to send and receive, to interpret might take some code. In an upcoming tip, I’ll show you an example with colors.

One response to “Tips: Why use UInt8”

  1. […] does not include an initializer for this value. Fortunately with some bit masking you learned in a previous tip its easy to convert from a hex value to a UIColor. Let me show you how, and a quick tip to get a […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: