Kotlin Bitwise Operators

Computers store every number as a row of tiny switches called bits. Each switch holds a 1 (on) or a 0 (off). Bitwise operators work directly on those switches. They run fast, they save memory, and they appear in graphics, networking, permissions, and game code.

Numbers as Light Switches

Imagine a panel with eight light switches. The number 12 turns on the switches worth 8 and 4. Binary notation shows the pattern.

 Switch value:   128  64  32  16   8   4   2   1
 Number 12:       0    0   0   0   1   1   0   0
                                   8 + 4 = 12

Kotlin writes binary numbers with the 0b prefix and prints them with toString(2).

val n = 0b1100
println(n)               // 12
println(12.toString(2))  // 1100

The Operators

Kotlin uses named functions instead of symbols. Each one sits between two values in infix style.

OperationNameMeaning
a and bAND1 only when both bits are 1
a or bOR1 when at least one bit is 1
a xor bXOR1 when the bits differ
a.inv()NOTFlips every bit
a shl nShift leftMoves bits left by n places
a shr nShift rightMoves bits right, keeps the sign
a ushr nUnsigned shiftMoves bits right, fills with zeros

AND, OR, and XOR in Action

Take the numbers 12 and 10. Line the bits up and compare each column.

        12 = 1 1 0 0
        10 = 1 0 1 0
 ---------------------
 12 and 10 = 1 0 0 0  = 8
 12 or  10 = 1 1 1 0  = 14
 12 xor 10 = 0 1 1 0  = 6
println(12 and 10)   // 8
println(12 or 10)    // 14
println(12 xor 10)   // 6

Shifting Bits

A left shift slides every bit to the left and fills the gap with zeros. Each step doubles the value. A right shift halves the value and drops the bit that falls off the edge.

 5      = 0 0 0 1 0 1
 5 shl 2= 0 1 0 1 0 0  = 20   (5 x 4)
 20 shr 2 = 0 0 0 1 0 1 = 5   (20 / 4)
println(5 shl 2)    // 20
println(20 shr 2)   // 5

Signed and Unsigned Shifts

Negative numbers carry a sign bit on the far left. The shr operator keeps that sign. The ushr operator pushes zeros into the left side regardless of the sign.

println(-16 shr 2)    // -4
println(-16 ushr 28)  // 15

Flipping Every Bit

The inv() function reverses all switches. The result for 5 equals -6, because negative numbers use a scheme called two's complement.

println(5.inv())   // -6

Real Use: Permission Flags

One Int can store many yes-or-no choices. Each choice owns one bit.

 READ  = 001  (1)
 WRITE = 010  (2)
 EXEC  = 100  (4)

 User with READ + WRITE = 011 (3)
const val READ = 1
const val WRITE = 2
const val EXEC = 4

fun main() {
    var permissions = READ or WRITE
    println((permissions and WRITE) != 0)   // true
    println((permissions and EXEC) != 0)    // false
    permissions = permissions or EXEC       // grant EXEC
    permissions = permissions and WRITE.inv()  // remove WRITE
    println(permissions)                    // 5
}

The or operator grants a permission. The and operator with inv() removes one. The and check tests whether a permission exists.

Quick Tricks

  • Check for an odd number with n and 1. The result is 1 for odd values.
  • Multiply by 8 with n shl 3.
  • Swap the roles of two flags using xor.

Readable code matters more than clever code. Use % and * when the goal is plain arithmetic, and keep bitwise operators for flags and binary data.