Bytes for Beginners (Part V.i): NOT



Swift Advanced Operators

In the previous Bytes for Beginners post, it was explained how binary numbers relate to their decimal equivalents in the context of a UInt8. In this post, I begin to look at advanced operators.

Advanced operators enable us to go beyond addition, subtraction, multiplication and division, and work with binary numbers in ways that are logical to the binary system. These operators are fully explained in Apple's documentation, but for those who like a bit more hands-on interactivity, below you will find the first example of an advanced operator.

NOT (~)

In this example, the blue row is the NOT equivalent of the green row and vice versa. A NOT is the inversion of a binary string, where all zeroes become ones and all ones become zeroes. In Swift a NOT is achieved with the prefix ~ placed in front of a UInt or binary number.

0b 0 0 0 0 0 0 0 0 Val
0 0 0 0 0 0 0 0 0
0b 1 1 1 1 1 1 1 1 Val
128 64 32 16 8 4 2 1 255

You can change the numbers by clicking on the blue or green squares. The blue and green rows display 8-bit binary numbers. Notice how the binary and its NOT equivalent always total 255 when added together (which is the maximum decimal value of a UInt8).

What's next?

As I write the JavaScript for further advanced operator examples, I'll post them.

Endorse on Coderwall