Ad space (banner)
๐ŸŽฏKotlin Lessons
Lesson 54 / 68

Bitwise Operators (and, or, xor, shifts)

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

Bitwise operations treat a number as binary and calculate bit by bit. Kotlin writes them as word-like infix functions: and, or, xor, and shl (left shift).

The sample code applies a and b, a or b, and a xor b to the integers 12 and 10, and shifts left by two bits with a shl 2, which multiplies by four. Unlike the & and | of other languages, the English words read easily - characteristically Kotlin.

A common stumbling block for beginners is that infix-function notation feels unfamiliar if you are used to the symbols (&, |) of other languages.

In real-world development they do not come up much in everyday app work, but they are valuable knowledge for performance-critical code.

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

๐Ÿงช This site can't compile or run Kotlin 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)