简体   繁体   English

~ 一元运算符和按位测试给出否定结果

[英]~ Unary Operator and Bitwise Tests Give Negative Results

I was studying bitwise operators and they make sense until the unary ~one's complement is used with them.我正在研究按位运算符,直到一元 ~one 的补码与它们一起使用,它们才有意义。 Can anyone explain to me how this works?谁能向我解释这是如何工作的?

For example, these make sense however the rest of the computations aside from these do not:例如,这些是有意义的,但是除这些之外的计算的 rest 没有:

1&~0 = 1   (~0 is 1 -> 1&1 = 1)
~0^~0 = 0  (~0 is 1 -> 1^1 = 0)
~1^0 = 1   (~1 is 0 -> 0^1 = 1)
~0&1 = 1   (~0 is 1 -> 1&1 = 1)
~0^~1 = 1  (~0 is 1, ~1 is 0 -> 1^0 = 1)
~1^~1 = 0  (~1 is 0 -> 0^0)

The rest of the results produced are negative(or a very large number if unsigned) or contradict the logic I am aware of.产生的结果的 rest 为负数(如果无符号,则为一个非常大的数字)或与我所知道的逻辑相矛盾。 For example:例如:

0&~1 = 0   (~1 = 0 therefor 0&0 should equal 0 but they equal 1)
~0&~1 = -2
~1|~0 = -1

etc. Anywhere you can point me to learn about this?等等。你可以指点我了解这个吗?

They actually do make sense when you expand them out a little more.当您将它们扩展一点时,它们实际上确实有意义。 A few things to be aware of though:不过有几点需要注意:

  1. Bitwise AND yields a 1 only when both bits involved are 1. Otherwise, it yields 0. 1 & 1 = 1, 0 & anything = 0.仅当涉及的两个位都是 1 时,按位与才会产生 1。否则,它会产生 0。1 & 1 = 1、0 & 任何东西 = 0。

  2. Bitwise OR yields a 1 when any of the bits in that position are a 1, and 0 only if all bits in that position are 0. 1 |当 position 中的任何位为 1 时,按位 OR 产生 1,仅当 position 中的所有位为 0 时才产生 0。 1 | 0 = 1, 1 | 0 = 1, 1 | 1 = 1, 0 | 1 = 1, 0 | 0 = 0. 0 = 0。

  3. Signed numbers are generally done as two's complement (though a processor does not have to do it that way.), Remember with two's complement.有符号数通常以二进制补码的形式完成(尽管处理器不必这样做。),请记住使用二进制补码。 you invert and add 1 to get the magnitude when the highest bit position is a 1.当最高位 position 为 1 时,您反转并加 1 以获得幅度。

Assuming a 32-bit integer, you get these results:假设是 32 位 integer,您会得到以下结果:

 0 & ~1 = 0 & 0xFFFFFFFE = 0
~0 & ~1 = 0xFFFFFFFF & 0xFFFFFFFE = 0xFFFFFFFE (0x00000001 + 1) = -2
~1 | ~0 = 0xFFFFFFFE & 0xFFFFFFFF = 0xFFFFFFFF (0x00000000 + 1) = -1

~1 = 0 - No it's not. ~1 = 0 - 不,不是。 It's equal to -2 .它等于-2 Let's take a eight bit two complement as example.让我们以一个八位二补码为例。 The decimal number 1 has the representation 0000 0001 .十进制数1具有表示0000 0001 So ~1 will have 1111 1110 which is the two complement representation of -2 .所以~1将有1111 1110这是-2的两个补码表示。

0&~1 = 0 ( ~1 = 0 therefor 0&0 should equal 0 but they equal 1 ) 0&~1 = 0 ( ~1 = 0因此0&0应该等于0但它们等于1 )

~1 equals -2 . ~1等于-2 If you flip all the bits of a Two's Complement number, you multiply it by -1 and subtract 1 from the result.如果翻转二进制补码数的所有位,则将其乘以-1并从结果中减去1 Regardless of that 0 has 0 for all the bits, so the result of & is going to be 0 anyway.不管0对于所有位都有0 ,所以&的结果无论如何都会是0

~0&~1 = -2

~0 has all bits set so ~0&~1 is just ~1 . ~0已设置所有位,因此~0&~1只是~1 Which is -2 .这是-2

~1|~0 = -1

~0 has all bits set, so the result of the | ~0已设置所有位,因此|的结果is ~0 (= -1 ) no matter what it is OR'd with.~0 (= -1 ),无论它与什么进行或运算。

For simplicity, just expanding upto 8 bits,为简单起见,只需扩展至 8 位,

1 = 0000 0001

You are assuming,你假设,

1 = 1111 1111 // which is wrong

1111 1111 , would be the maximum possible number. 1111 1111 ,将是最大可能的数字。

If you want every bit set to 1 , you need to do:如果您希望每个位都设置为1 ,您需要执行以下操作:

(-1)

or或者

~0

Also be careful with the types you use, like you may want to do operation of 64 bits, but it only expanded upto 32 bits.还要小心你使用的类型,比如你可能想做 64 位的操作,但它只能扩展到 32 位。 You need to typecaste in such cases.在这种情况下,您需要进行类型转换。

For example:例如:

uint64_t a = 1 << 63;

would result into 0 if 1 expanded to 32 bit, but you shifted it by 63.如果1扩展为 32 位,则结果为 0,但您将其移动了 63。

So to correct that,因此,为了纠正这一点,

uint64_t a = 1ULL << 63;

or或者

uint64_t a = (uint64_t)1 << 63;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM