代码之家  ›  专栏  ›  技术社区  ›  Zach Smith

掩蔽位说明

  •  1
  • Zach Smith  · 技术社区  · 15 年前

    使用 AND 二者对立的逻辑:

         10101010
    AND  01101001
         ________
         00101000
    

    以及 )或关闭(使用 OR )我是真的改变了任何一个位,还是只是用 AND/OR 逻辑?

    3 回复  |  直到 12 年前
        1
  •  1
  •   Gustavo Mori    13 年前

    要启用(1),可以在要启用的位置使用带1的OR运算符,因为无论流中的原始值是多少,结果都将启用

       00000000 // whatever the values in the input
    OR 00000001 // 'OR' turns on the last position in the stream
       --------- 
       00000001
    

    要关闭(0),可以在要关闭的位置使用带有0的AND运算符,因为无论输入流中的原始值是多少,结果都将关闭。

        11111111 // whatever the values here
    AND 11111110 // turns off the last position in the stream
        ---------
        11111110
    
        2
  •  0
  •   Zach Smith    15 年前

    其他人,如果我错了,请纠正我:

    要打开8位流中的第4位,可以使用 OR 逻辑使用 00001000

    要关闭8位流中的第4位,可以使用 AND 11110111 .

    切换要使用的位 11111111 使用 XOR 逻辑。

        3
  •  0
  •   Charles E. Grant    15 年前

    在大多数语言中,除了二进制操作外,还必须有赋值。

    foo = get_byte() // Call some function to get the original value of foo
    foo = foo AND 11110111 // Replace foo with the result of the AND, which
                           // in this case will turn off the 4th bit, and leave
                           // the other bits unchanged
    

    最后一行用二进制运算的结果替换foo的内容