How do negative operands of bitwise operators work in Java?
•
Java
-4 & -5 = -8 // How?
-4 & -5 = -8 // How? -4 & 5 = 4 // How?
I need to explain how to achieve the above results I have no difficulty in solving with positive integers
Solution
Simply convert an integer to a binary representation (for negative integers, use two's complexity) and run and bitwise:
-4 11111..1100 & -5 11111..1011 -8 11111..1000 -4 11111..1100 & 5 00000..0101 4 00000..0100
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码