Bitwise OR

Bitwise OR is written using a single pipe: |. Whereas bitwise AND returns the new number if both bits are set to 1, bitwise OR returns the new number if either bit is equal to 1 (see Figure 7.5).

Image

Figure 7.5 Performing a bitwise OR on two bytes with the result in the middle.

When you use the bitwise OR operator, the result is all 1s (255). You can try this out with code like so:

var a:UInt8 = 0b11111010 //250var b:UInt8 = 0b01010111 //87a|b //255

Figure 7.6 shows another example where both inputs are 0, so the resulting bit is 0 also.

Figure 7.6 Another bitwise OR comparing two bytes.

You could write ...

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.