Ad space (banner)
๐ŸฆSwift Lessons
Lesson 54 / 68

Bitwise Operators (AND, OR, XOR, shifts)

In this lesson you will learn the basics of bitwise operators in Swift, and understand what it means to work with a number bit by bit. This is for anyone searching for "Swift bitwise operators how to use".

Bitwise operations treat a number as binary and calculate bit by bit. There is & (AND), | (OR), ^ (XOR), and << (left shift), and they show up in permission flags and low-level work.

The sample code applies &, |, and ^ to the integers 12 and 10, and shifts left by two bits with a << 2, which multiplies by four. Thinking in binary makes each operation much easier to follow.

A common stumbling block for beginners is confusing the logical operators &&/|| with the bitwise &/|. Use &&/|| for conditionals and &/|/^ for bit-level work.

In real-world development they do not come up much in everyday app work, but managing flags as bits is still genuinely useful in embedded devices with limited memory.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Swift directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)