Bytes for Beginners (Part V.iii): OR (|)



Swift Advanced Operators

Hot on the heels of the last bytes for beginners post on AND (&), here's OR (|).

OR (|)

In this example, the blue and green rows are independent of one another while the grey row shows the result of an OR between the two. In Swift an OR is achieved with the infix operator | placed between two integers. The result is that a bit set to 1 in either number sets the bit to 1 in the resulting 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

Result

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. The grey row shows the result.

What's next?

Once again, as I write the JavaScript for further advanced operator examples, I'll post them. But most likely not again today. Two in one day is quite enough! And besides I don't want to interrupt the Apple special event.