Bitwise operators
Bitwise operators enable us to manipulate the individual bits of a value. One of the advantages of bitwise operators is that they are directly supported by the processor and so can be significantly faster than basic arithmetic operations like multiplication and division. We will see how to do basic multiplication and division using bitwise shift operators later in this chapter.
Before we look at what we can do with bitwise operators, we will need to have the ability to show the binary representation of our variables in order to see what the operators are doing. Let's take a look at a couple of ways that we can do this.
Printing binary numbers
Apple provides us with a generic initializer for the String
type that will provide us with the string representation of a given value. This initializer is init(_:radix:uppercase:)
. By default uppercase
is set to false
and radix
is set to 10
. The radix
defines the number base that will be displayed, where the...