views:

31

answers:

2

I have a quick question about masking bits. If I want to turn two 8 bit streams on, do I

use the AND logic against the two:

     10101010
AND  01101001
     ________
     00101000

or do I actually change one of the bits in the stream to turn the bits on? I guess my question is when I'm turning on (using AND) or turning off (using OR) do I actually change any of the bits, or just comparing the two using the AND/OR logic?

A: 

Others, correct me if I am wrong:

To turn ON the 4th bit in an 8bit stream you would compare the 8bit stream using the OR logic using 00001000.

To turn OFF the 4th bith in an 8bit stream you would compare the 8bit stream using the AND logic using 11110111.

To toggle the bit you would use 11111111 using the XOR logic.

HollerTrain
That `XOR` would toggle every bit in the "stream".
Sapph
@Sapph, yes correct. So if i want to turn on a specific bit I would use `00000001` (with this placing the first bit that we are comparing against this steam) into an `AND` logic scenario, but I can also use this method when simply comparing two bit streams (not trying to turn on/off one single bit)?
HollerTrain
A: 

I'm not sure what you mean by 'streams' in this case.

In most languages you are going to have to have an assignment as well as the binary operation.

That is you would typically have something like

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

The last line replaces the contents of foo with the results of the binary operation

Charles E. Grant