This question already has answers here:
What are bitwise operations?
                                
                                    (4个答案)
                                
                        
                                5年前关闭。
            
                    
有人可以解释一下为什么System.out.println((12 & 9));打印8吗?

在这种情况下,我不了解&运算符的逻辑。

谢谢!

最佳答案

It is Binary representation. (Machine Understandable language)
Here first place resembles  value - 1 (Right to Left)
Second place resembles  value - 2 (Right to Left)
Third place resembles  value - 4 (Right to Left)
Fourth place resembles  value - 8 (Right to Left)


&说1和1为true(即1),其余组合为false(即0)

     12 - 1100
      9 - 1001
===============
      8 - 1000

10-06 09:36