为什么10 ^ 1是11? [英] Why 10^1 is 11?
本文介绍了为什么10 ^ 1是11?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在学习 C ++ 。我尝试使用以下表达式计算整数的幂:
val = 10 ^ 1;
而不是预期的回答 10
11
。我已通过使用 pow
函数修复了math.h库的问题,但我想知道为什么这个语句给我错误的结果。
<因为^是排它或运算符,而不是求幂运算符。基本上,因为二进制中的最后一位是0,所以通过应用异或1,最后一位被转换为1,因为它不同于0。
I am currently learning C++. I was trying to compute power of an integer using the expression:
val=10^1;
Instead of expected answer 10
, the result was 11
. I have fixed the problem by using pow
function of math.h library but I am wondering why this statement is giving me the wrong result.
解决方案
Because ^ is the exclusive or operator and not the exponentiation operator. Basically, because the last bit of 10 in binary is 0, by applying exclusive or of 1 the last bit gets converted to 1 because it is different than 0.
这篇关于为什么10 ^ 1是11?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文