Bitwise AND

Bitwise AND is represented by a single ampersand: &. Whereas bitwise NOT inverts all the bits, bitwise AND changes the bits only if both bits are 1s. For example, say that you have something like what’s shown in Figure 7.3.

Image

Figure 7.3 Performing a bitwise AND on two bytes.

In this example, the result of the bitwise AND combination is 0 because there is no place where both bit 1 and bit 2 contain 1s in the same slot. You could code this example like so:

var a:UInt8 = 0b10101010 //170var b:UInt8 = 0b01010101 //85a&b //0

Figure 7.4 shows another example of a bitwise AND combination that has some positive ...

Get Learning Swift™ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.