按位不 - 不是我的预期 [英] bitwise not - not what I expected

查看:61
本文介绍了按位不 - 不是我的预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个函数将带有二进制数字a1 ...的数字带到带有二进制数字b1 ... bn的

数字,其中如果ai为0,则每个bi为1,并且副#
$ b $反之亦然? (例如,函数'18的值[二进制10010]将是13

[二进制01101]。)我认为这是代字号运算符(〜)所做的,但是当我

去尝试它我发现事实并非如此。我通过实验发现(并且通过查看文档验证了

),代字号运算符将n / $
取为 - (n + 1)。我无法想象与二进制数字有什么关系。任何人都可以对这个问题有所了解吗? (如果你很好奇,我正在编写一个脚本,这将是玩bim玩Nim,就像熟悉按位操作符一样。好的

的东西也是:我以为我理解他们,但显然我不是。)


Muchas gracias提供任何帮助和提示。


和平,

EJ

Is there a function that takes a number with binary numeral a1...an to the
number with binary numeral b1...bn, where each bi is 1 if ai is 0, and vice
versa? (For example, the function''s value at 18 [binary 10010] would be 13
[binary 01101].) I thought this was what the tilde operator (~) did, but when I
went to try it I found out that wasn''t the case. I discovered by experiment (and
verified by looking at the documentation) that the tilde operator takes n
to -(n+1). I can''t imagine what that has to do with binary numerals. Can anyone
shed some light on that? (In case you''re curious, I''m writing a script that will
play Nim, just as a way of familiarizing myself with bitwise operators. Good
thing, too: I thought I understood them, but apparently I don''t.)

Muchas gracias for any and all helps and hints.

Peace,
EJ

推荐答案

Elaine Jackson写道:
Elaine Jackson wrote:
是否有一个函数将带有二进制数字a1 ...的数字带到带有二进制数字b1 ... bn的
数字,其中如果ai为0,则每个bi为1,反之亦然? (例如,函数'18的值[二进制10010]将是13
[二进制01101]。)我以为这是代字号运算符(〜)所做的,但是当我去了
时试一试,我发现事实并非如此。我通过实验发现(并通过查看文档验证),代字号运算符采用n - /(n + 1)。我无法想象与二进制数字有什么关系。
Is there a function that takes a number with binary numeral a1...an to the
number with binary numeral b1...bn, where each bi is 1 if ai is 0, and vice
versa? (For example, the function''s value at 18 [binary 10010] would be 13
[binary 01101].) I thought this was what the tilde operator (~) did, but when I
went to try it I found out that wasn''t the case. I discovered by experiment (and
verified by looking at the documentation) that the tilde operator takes n
to -(n+1). I can''t imagine what that has to do with binary numerals.




它与二进制数有很大关系!谷歌为两个的补充。


在此期间,试试这个:



It has a lot to do with binary! Google for "two''s complement".

In the meantime, try this:

~18& 31
~18 & 31



13


''〜''运算符不关心精度 - 也就是说,您正在操作或预期结果中有多少位

。在你的例子中,你

将小数18表示为''10010'',但''000000010010''也是正确的,

对吧?


在两个'的补码数学中,两个反转,''01101''和''111111101101''

分别相当于十进制-19。


使用长度为''n'的掩码并确保你只需要
得到最低有效n位 - 这就是你''重新寻找。

因为你在你的例子中操作五位,我选择小数31,

或''11111''。


- 格雷厄姆


13

The ''~'' operator cannot care about precision -- that is, how many bits
you''re operating on, or expecting in your result. In your example, you
represent decimal 18 as ''10010'', but ''000000010010'' is also correct,
right?

In two''s complement math, both inverses, ''01101'' and ''111111101101''
respectively, are equivalent to decimal -19.

And-ing with a mask that is the of length ''n'' will ensure that you only
get the least significant n bits -- and this is what you''re looking for.
Since you''re operating on five bits in your example, I chose decimal 31,
or ''11111''.

-- Graham




" Elaine Jackson" < EL *************** @ home.com> schrieb im Newsbeitrag

新闻:1Y *********************** @ news3.calgary.shaw。 ca ...

....

"Elaine Jackson" <el***************@home.com> schrieb im Newsbeitrag
news:1Y***********************@news3.calgary.shaw. ca...
....
我正在编写一个将播放Nim的剧本,这只是一种熟悉的方式我自己的位运算符。
好​​东西也是:我以为我明白了,但显然我不是。)
I''m writing a script that will
play Nim, just as a way of familiarizing myself with bitwise operators. Good thing, too: I thought I understood them, but apparently I don''t.)



你好Elaine,

你已经描述过一个普遍的误解,你似乎并不是唯一一个住在一起的b $ b。

已发布的启发性答案可能是sufficwe,但是一个schuld

liek添加更多启蒙:

位补充与set complents和aritmetic

否定有很大关系(有时称为2'补码明显的原因)。考虑

红色的集合。 蓝色现在补充什么? "绿色"和

黄色显然是错误的答案。你实际上不能给出任何答案

你定义你正在处理的总集合。这同样适用于

逻辑位操作。通常你会选择处理器。要定义的单词或部分

。一些高级语言更灵活;甚至一些

计算机(矢量处理器)都是。


唯一的规则是,〜(~x)== x


与数字相同的情况:+5的否定是什么。你必须非常努力地想要
!这是一个棘手的问题,你可能会给出一个

技巧答案:-5。你应该知道这只是一个技巧。 -5

不包含任何其他信息,因为它是一些补充信息。 5.(同样

复杂的虚构数字:5j(在Python中)只是说它有点花哨

5.)

现在我们定义正数和位模式之间的转换5 =

LoL。注意5 == ... 000005或LoL == .... ooooLoL对任何

的理解没有帮助,所以你通常会跳过这部分。


现在你做一些算术反转:5 - > -5但是这可以(并且

应该)保留处理器的秘密!绝不应该对机器如何表示-5感兴趣

。如果你很勇敢,那就知道

曾经有一段时间计算机将-5表示为...... LLLoLo。是的

有效!你有两个不同的零然后:+ 0和-0 !!!!


大多数计算机不区分否定

数字和补充集合的表示(更不用说注意了特殊的总集合

complemt指的是)。因此,秘密是指秘密。现在两个补码

计算机算术总是向你透露。


注意,在掩盖之类的东西中没有用处。 MSB,即

位补码仅适用于31位。这将导致~5 == ~LLO ==〜

LL..LLLLoL == 2,147,483,643没有太大改进,嗯!?


亲切地/>
Michael P


Hi Elaine,
You have described a general misconception you seem to be not the only one
to live with.
The enlightening answers having been posted might sufficwe, but a schuld
liek to add some more "enlightenment":
Bit complements have a lot to do with set complents and the aritmetic
negation (sometimes called two''s complement for obvious reasons). Consider
the set of of "red" of "blue". Now whats the complement? "green" and
"yellow" is obviously the wrong answer. You in fact cannot give any answer
befor you define the total set you are dealing with. The same applies to
logical bit operations. Generally you take a "processor" word or a part of
it to be defined. Some high level languages are more flexible; and even some
computers ("vector processors") are.

The only rule is, that ~(~x) == x

The same situation with numbers: What is the negation of +5. You have to
think very hard! This is a trick question and you probably will give a
"trick answer": -5. You should be aware that this is just a trick. "-5"
contains no other information as that it is some "complement" of 5. (same
with complex "imaginary" numbers: 5j (in Python) just says it is some fancy
5.)

Now we define a transformation between positive numbers and bit patterns 5 =
LoL. Note that 5 == ...000005 or LoL == ....ooooLoL does not help any
understanding so you generally skip this part.

Now you do some arithmetic "inversion": 5 -> -5 This however can (and
should) stay a secret of the processor! By no means should you be interested
in how the machine represents "-5". If you are courious then know that
there had been times when computers represente -5 as ...LLLoLo. Yes it
worked! And you had two diffrent "zeros" then: +0 and -0 !!!!

Most computers do not distinguish between the representation of negativ
numbers and complemented sets (let alone note a special "total set" the
complemt was referring to). Thus the "secret" of modern two''s-complement
computern arithmetic is always disclosed to you.

Note that there is no use in something like "masking" the MSB, i.e. that
bits-complements only work on 31 bits. This will lead to ~5 == ~LoL == ~
LL..LLLLoL == 2,147,483,643 Not much improvement, eh!?

Kindly
Michael P


虽然其他人解释了〜运算符的工作原理,但我建议另外一种可能性:b

While others explained how the ~ operator works, let me suggest
another possibility: the bitwise exclusive or.
def bin(i):
.... l = [''0000' ',''0001'',''0010'','0011'',''0100'',''0101'',''0110'',''0111'',

....'''1000'','''1001'',''1010'',''1011'',''1100'',''1101'',''1110'','' 1111'']

.... s =''''。join(map(lambda x,l = l:l [int(x,16)],hex(i)[2 :]))

....如果s [0] ==''1''和i> 0:

.... s =''0000''+ s

....返回s

.... bin (18)
''00010010''~18
-19 bin(~18)#tickyy ......
''11111111111111111111111111101101''~18& 0x1f
13 bin(~18& 0x1f)
''00001101''18 ^ 0x1f #XOR!
13 bin(18 ^ 0x1f)#XOR
''00001101''
def bin(i): .... l = [''0000'', ''0001'', ''0010'', ''0011'', ''0100'', ''0101'', ''0110'', ''0111'',
.... ''1000'', ''1001'', ''1010'', ''1011'', ''1100'', ''1101'', ''1110'', ''1111'']
.... s = ''''.join(map(lambda x, l=l: l[int(x, 16)], hex(i)[2:]))
.... if s[0] == ''1'' and i > 0:
.... s = ''0000'' + s
.... return s
.... bin(18) ''00010010'' ~18 -19 bin(~18) # tricky... ''11111111111111111111111111101101'' ~18 & 0x1f 13 bin(~18 & 0x1f) ''00001101'' 18 ^ 0x1f # XOR! 13 bin(18 ^ 0x1f) # XOR ''00001101''



您还需要考虑这个数字你要反转的位数。

x ^ 0x1f反转x的5个最低有效位。

x ^ 0xff反转x的8个最低有效位,依此类推。

- Irmen de Jong


You still have to think about the number of bits you want to invert.
x ^ 0x1f inverts the 5 least significant bits of x.
x ^ 0xff inverts the 8 least significant bits of x, and so on.
--Irmen de Jong


这篇关于按位不 - 不是我的预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆