如何位运算符的结果发生? [英] How do bitwise operator results occur?

查看:231
本文介绍了如何位运算符的结果发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很惊讶,我无法找到答案在谷歌这个简单的探测问题。检查大约十几个不同的页面,我只是要问这里后......

I'm quite surprised that I can't find an answer to this simple sounding question on Google. After checking about a dozen different pages I'm just going to ask here ...

根据此页面,3 &安培; 5导致1。此外,3 | 5结果7.我唯一的问题很简单:

According to this page, 3 & 5 result in 1. Also, 3 | 5 result in 7. The only question I have is simply:


  • 我们如何为3和送1; 5?

  • 如何我们得到7 3 | 5?

此外,何谈负数?


  • 如何8安培; -8导致8?

果然,使用Java语言编写以下内容:

Sure enough, writing the following in java:

System.out.println(3&5);
System.out.println(3|5);
System.out.println(8&-8);

产生这样的输出:

Produces this output:

1
7
8

但同样,如何这些结果来确定/计算的?

But again, how are these results determined / calculated?

推荐答案

3及5:

0011
0101
----- AND
0001 == 1

3 | 5:

0011
0101
----- OR
0111 == 7


在Java中否定被定义为

二的补的否定(这是非常常见的) 。结果
因此, -x =〜X + 1 =〜(X - 1)。


Negation in Java is defined to be two's complement negation (which is extremely common).
So -x = ~x + 1 = ~(x - 1).

8和; -8:

00001000 //8
11111000 //-8
-------- AND
00001000 // 8

使用否定的最后一个定义,首先-1通过所有最右边的零借用(如果有的话),将它们设置为它去,直到它击中1,它重置,任何的它的左边是左修改。补然后恢复最右边的零和最右边的一(所有这些由-1得到有效补充),一切补充到左右端之一:

Using the last definition of negation, the -1 first borrows through all rightmost zeroes (if there are any), setting them as it goes, until it hits a 1, which it resets, anything to the left of that is left unmodified. The complement then restores the rightmost zeroes and the rightmost one (all of which were effectively complemented by that -1), and complements everything to the left of the rightmost one:

00001000 // 8
00000111 // 8 - 1 = 7
11111000 // -8

注意-8仅仅是11111000,如果你有8位数字的工作。如果您有更多的位,将有更多的1的左边。如果你只有4位,你遇到某种麻烦,因为-8原来有相同的再presentation 8,所以-8(4位数学)一个数字,是其自身的负(像零)。

Note that -8 is only 11111000 if you're working with 8 bit numbers. If had you more bits, there would be more 1's to the left. If you have only 4 bits, you run into some kind of trouble because -8 turns out to have the same representation as 8, and so -8 is (in 4 bit math) a number that is its own negative (like zero).

其实,8是不是一个很好的例子,因为它太简单了。让我们做 100安培; -100 (百,而不是4个):

Actually, 8 is not a very good example, because it's too simple. Let's do 100 & -100 (hundred, not 4):

01100100 // 100
01100011 // 99
10011100 // -100

现在和放大器; 100:

Now & with 100:

01100100 // 100
10011100 // -100
-------- AND
00000100 // 4

在一般情况下, X'放大器; -x 隔离最右边1.无论是最右边的不是最右边的1是受到否定,所以对于一些只是部分零,它看起来像你这样做 X'放大器; X (这当然是 X )。上半部分,到了最右边的左边,是补充,让你处处有1变成0,到处你有1变为0 0安培; 1 = 0 ,所以,让0无处不在。

In general, x & -x isolates the rightmost 1. Neither the rightmost zeroes not the rightmost 1 are affected by negation, and so for only that part of the number, it looks like you're doing x & x (which is of course x). The upper part, to the left of the rightmost one, is complemented, so everywhere you had a 1 becomes 0, and everywhere you had a 1 becomes 0. 0 & 1 = 0, so that gives 0 everywhere.

这篇关于如何位运算符的结果发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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