Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

go

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.

like image 740
BufBills Avatar asked Dec 17 '25 17:12

BufBills


2 Answers

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
like image 116
icza Avatar answered Dec 20 '25 11:12

icza


It is an Arithmetic operators:

^    bitwise XOR            integers

You can see it on this Bitwise Calculator

Result in binary    111
like image 23
VonC Avatar answered Dec 20 '25 11:12

VonC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!