How do bitwise operator results occur?

跟風遠走 提交于 2019-12-02 23:35:25

问题


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 ...

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

  • How do we get 1 for 3 & 5?
  • How to we get 7 for 3 | 5?

Also, what about negative numbers?

  • How does 8 & -8 result in 8?

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?


回答1:


3 & 5:

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

3 | 5:

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

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

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

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).

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

Now & with 100:

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

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.




回答2:


3 & 5 => 1

3 in binary is 0011.

5 in binary is 0101.

apply the bitwise and

  0011
& 0101
------
  0001 => 1 in decimal

Take the same idea, along with the truth tables for each op, and apply them to your specific problems.




回答3:


You need to turn the number into binary at that point you need to remember that "b1 and b2=1" only if they are both 1, and "b1 or b2=0" only if they are both 0.

So for example

5 or 3 = 101 or 011 = 111 = 7
5 and 3 = 101 and 011 = 001 = 1


来源:https://stackoverflow.com/questions/20169287/how-do-bitwise-operator-results-occur

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!