Why Does 5 ^ 2 Equal 7 in Go?

in go, `5 ^ 2` evaluates to `7` because the `^` symbol does **not** mean exponentiation—it’s the **bitwise xor operator**. this is a common source of confusion for developers coming from languages like python (`**`) or javascript (where `^` *also* means xor, but beginners often assume it’s power). let’s break it down:

Binary representation:

  • 5 in binary is 101
  • 2 in binary is 010 (aligned to same bit width)

XOR compares each bit: result is 1 only when bits differ:

  101  (5)
^ 010  (2)
-----
  111  (7)

So 5 ^ 2 == 7 is mathematically correct under bitwise logic.

⚠️ Important notes:

  • Go has no built-in exponentiation operator. To compute powers like 5², use math.Pow(5, 2) (returns float64) or implement integer exponentiation manually.
  • ^ is also used for bitwise complement when unary (e.g., ^x flips all bits of x), but as a binary operator, it’s always XOR.
  • Always double-check operator precedence: ^ has lower precedence than +/- but higher than ==, so 5 ^ 2 == 7 is parsed as (5 ^ 2) == 7 — which safely evaluates to true.

✅ Pro tip: Use fmt.Printf("%b", 5^2) to verify bitw

ise results during debugging.