在Go语言中,为什么这个表达式的结果是7?assert(5^2 == 7)

huangapple go评论78阅读模式
英文:

in go lang, why this is 7 ? assert(5^2 == 7)

问题

在Go语言中,为什么结果是7?

assert(5^2 == 7)

太奇怪了。
我试着在谷歌上搜索,但谷歌对特殊字符的处理效果不好。
谢谢。

英文:

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.

答案1

得分: 11

^ 运算符是异或(exclusive OR)运算符,如果操作数是数字,则按位应用。

5 = 101b // 二进制表示,但 Go 语言没有二进制字面量
2 = 010b
异或运算:
7 = 111b
英文:

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

答案2

得分: 7

这是一个算术运算符

^    按位异或            整数

你可以在这个按位计算器上看到它

二进制结果 	111
英文:

It is an Arithmetic operators:

^    bitwise XOR            integers

You can see it on this Bitwise Calculator

Result in binary 	111

答案3

得分: 2

如其他人已经描述的那样,^ 是一个用于异或运算的操作符。如果你想计算 5 的平方,可以使用 math.Pow() 函数。

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("5 的平方 = %f\n", math.Pow(5,2))
}
英文:

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\n", math.Pow(5,2))
}

huangapple
  • 本文由 发表于 2015年2月2日 03:06:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/28266589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定