1

我有一个关于屏蔽位的快速问题。如果我想打开两个 8 位流,我要

对两者使用AND逻辑:

     10101010
AND  01101001
     ________
     00101000

还是我实际上更改了流中的一位以打开这些位?我想我的问题是当我打开(使用AND)或关闭(使用OR)时,我是否真的改变了任何位,或者只是使用AND/OR逻辑比较两者?

4

3 回答 3

1

要打开 (1),您可以在要打开的位置使用带有 1 的 OR 运算符,因为无论流中的原始值是什么,结果都是 ON

   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
于 2011-05-16T18:37:50.010 回答
0

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

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

要关闭 8 位流中的第 4 位,您可以使用AND逻辑比较 8 位流11110111

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

于 2010-02-12T00:18:43.643 回答
0

在这种情况下,我不确定您所说的“流”是什么意思。

在大多数语言中,您将不得不进行赋值以及二进制操作。

那就是你通常会有类似的东西

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 的内容替换为二元运算的结果

于 2010-02-12T00:56:21.957 回答