无法理解代码,特别是&

dga*_*ma3 0 java bit-manipulation

我正在阅读有关Java pacman游戏的教程.

这是有问题的代码.

   if (pacmanx % blocksize == 0 && pacmany % blocksize == 0) {
        pos = // integer         
        ch = screendata[pos];

        if ((ch & 16) != 0) { // do not understand this.
            screendata[pos] = (short)(ch & 15);
            ...
        }
Run Code Online (Sandbox Code Playgroud)

我并不是真的了解单身&.我理解这个操作数检查if语句的两侧,或者是一个按位运算符.但是,根据以下测试,它似乎不是:

if I was to test (ch = 18):
(ch & 16) = 16
(ch & 8) = 0
(ch & 2) = 2
Run Code Online (Sandbox Code Playgroud)

谢谢

Baz*_*Baz 8

&按位运算符 AND:

18 = 10010
16 = 10000
----------
16 = 10000


18 = 10010
 8 = 01000
----------
 0 = 00000
Run Code Online (Sandbox Code Playgroud)

因此,if将检查第五位是1还是0.