按位OR? [英] Bitwise OR?
本文介绍了按位OR?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么3500 | -67等于3500而不是-3567?
Why is 3500 | -67 equal to 3500 instead of -3567?
推荐答案
xkenneth schrieb:
xkenneth schrieb:
为什么是3500 | -67等于3500而不是-3567?
Why is 3500 | -67 equal to 3500 instead of -3567?
它不是 - 它的-67。
Diez
It isn''t - its -67.
Diez
xkenneth写道:
xkenneth wrote:
为什么3500 | -67等于3500而不是-3567?
Why is 3500 | -67 equal to 3500 instead of -3567?
那很有趣......在我的电脑上,python说它是-67。 Java也是
表示它是-67。
Haven尚未查看两个值的实际位数。
你有什么Python实现和什么机器?
--Tim
Well that''s funny... On my computer, python says it''s -67. Java also
says it''s -67.
Haven''t yet looked at the actual bits of both values.
What Python implementation and what machine do you have?
--Tim
实际上,跟进我自己的回复:
3500 | 67 = 3567.所以也许你的期望值为3500 |
-67。
但是试试
-3500 | -67
为了好玩:它是-3
按位或没有算术,如果你想预测一些关于
的东西
得到的整数,你应该知道它们是如何存储的。
负整数以2补码格式存储:减1将所有
位设置为1.关闭位使其成为一个更负的数字(因为你没有触摸符号位,所以
长)并转向更多bit on on
a较小的负数。
对于正数3500和67执行按位或运算结果为3567:
证明它们没有重叠位。
知道3500和-67只有重叠位:
因此结果是-67。 (将3500中的位添加到
-67的位中并没有打开任何额外的位。)
使用位运算符来操作位,并将操作数视为集合
位。按位OR是两组位的并集。
不要把操作符想象为操作符作为数字,不要尝试
来做你的使用按位求和或者!
:-)
--Tim
Actually, following up to my own reply:
3500 | 67 = 3567. So perhaps that sets your expectations for 3500 |
-67.
But try
-3500 | -67
for fun: it is -3
Bitwise or is no arithmetic and if you want to predict something about
the resulting integers, you should know something about how they are
stored.
Negative integers are stored in 2-complement format: minus 1 is all
bits set to 1. Turning bits off makes it a more negative number (as
long as you don''t touch the sign-bit) and turning more bits on makes it
a smaller negative number.
Doing bitwise-OR for the positive numbers 3500 and 67 results in 3567:
proof that they don''t have any overlapping bits.
Know it turns out that 3500 and -67 have only overlapping bits:
therefore the result is -67. (adding the bits from 3500 to the bits of
-67 doesn''t turn on any extra bits).
Use bit operators to manipulate bits, and think of the operands as sets
of bits. Bitwise OR is the union of 2 sets of bits.
Don''t think of the operands to bit operators as numbers, and don''t try
to do your sums using bitwise or!
:-)
--Tim
>
这篇关于按位OR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文