in go lang, why this is 7 ?
assert(5^2 == 7)
so strange. I tried to google it. but google does not work well with special characters. Thanks.
The ^
operator is the XOR (exclusive OR), applied bitwise if operands are numbers.
5 = 101b // in binary, but Go doesn't have binary literals
2 = 010b
XOR:
7 = 111b
It is an Arithmetic operators:
^ bitwise XOR integers
You can see it on this Bitwise Calculator
Result in binary 111
As others already described, ^ is an operator for XOR. If you want to calculate the square of 5, you can use math.Pow() function.
package main
import (
"fmt"
"math"
)
func main() {
fmt.Printf("square of 5 = %f
", math.Pow(5,2))
}