iOS ● ● ● Swift 6

Kodebits Day 58: Custom Operator

Jul 14 2026
Practice operators with a short swift challenge.

What is the output?

infix operator **:
  MultiplicationPrecedence
func ** (b: Int, e: Int) -> Int {
  var r = 1
  for _ in 0..<e { r *= b }
  return r
}
print(2 ** 3)
print(5 ** 2)


Try it in the online Swift Playground →

[spoiler title="Solution"]

Answer:

8
25

Explanation:

Swift allows custom operators with specified precedence. Here ** implements exponentiation with MultiplicationPrecedence.

[/spoiler]


Further Reading