How do bitwise operator results occur?

I am very surprised that I can not find the answer to this simple audio question on Google. After checking dozens of different pages, I just ask here ...

According to this page , 3 and 5 lead to 1. Also 3 | 5 result 7. The only question I have is simple:

  • How do we get 1 for 3 and 5?
  • How do we get 7 on 3 | 5?

Also, what about negative numbers?

  • How do 8 and -8 lead to 8?

Of course, writing the following in java:

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

Produces this conclusion:

 1 7 8 

But then again, how are these results defined / calculated?

0
source share
3 answers

3 and 5:

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

3 | 5:

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

Denial in Java is defined as two complementary denials (which is extremely common). So -x = ~x + 1 = ~(x - 1) .

8 and -8:

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

Using the last definition of negation, -1 first takes all the rightmost zeros (if any), setting them as he goes, until he reaches 1, which he discards, everything that is left of him remains unchanged. Then the complement restores the rightmost zeros and the rightmost one (all of them are effectively supplemented by -1) and supplements everything to the left of the rightmost one:

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

Please note that -8 is only 11111000 if you are working with 8-bit numbers. If you had more bits, there would be more than 1 on the left. If you have only 4 bits, you are faced with some kind of problem, because -8 got the same representation as 8, and therefore -8 (in 4-bit math) a number, which is its own negative (for example, zero) .

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

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

Now with 100:

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

In the general case, x & -x isolates the rightmost 1. None of the rightmost zeros, not the rightmost 1, is independent of negation, and therefore only for that part of the number it looks like you are doing x & x (which of course, x ) . The upper part, to the left of the rightmost one, is supplemented, so wherever you have 1, it becomes 0, and wherever you have 1, it becomes 0. 0 & 1 = 0 , so it gives 0 everywhere.

+6
source

3 and 5 => 1

3 in binary 0011.

5 in binary format - 0101.

apply bitwise and

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

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

+1
source

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

So for example

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

Source: https://habr.com/ru/post/1013546/


All Articles