Bytes for Beginners (Part V.ii): AND (&)



Swift Advanced Operators

It's been a while, but in the last Bytes for Beginners post I explored the NOT (~) operator and promised to continue creating bitwise operator examples in order to explain them in an interactive way.

Apologies for the delay, but better late than never here's the next one in the series.

AND (&)

In this example, the blue and green rows work independently of one another while the grey row is the AND result of the two. This means that if both binary numbers have a position set to 1 then the result will also have that bit set to 1. Otherwise it will be 0. In Swift an AND is achieved with the infix operator & placed between integers of a length of between 8 and 64 bits in length.

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 0 0 0 0 0 0 0 0 Val
0 0 0 0 0 0 0 0 0

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 squares show the result.

What's next?

Hopefully there won't be such a long delay before the next post.