Davio
Davio

Reputation: 4737

What does Xor -> And -> Xor do?

I'm having trouble with an algorithm.

I have a byte used for IO of which certain bits can be set with a method called XorAndXor. The algorithm works as follows:

newValue = (((currentValue XOR xorMask1) AND andMask) XOR xorMask2)

The description reads:

If both xor-masks have the same value then this function inserts the bits of the xor-mask into the bit locations where the and-mask is 1. The other bits remain unchanged.

So what I expect from this function is when I have the following byte: 00101101 and I use 01000000 for both xor-masks and as the and-mask, that only the second bit would be set to 1 and the result would be 01101101.

However, when doing the math and going through the functions, the result is 00000000.

What am I doing wrong or is there something about this function that I don't understand? This kind of low level programming has been a while so I don't really know if this is a methodology used often and why and how you should use it.

Let me just ask this simple question: Is there a way to use this function effectively to set (or unset/change) a single bit (without asking specifically for the current value)?

For example: The current value is 00101101 (I don't know this), but I just want to make sure the second bit is set, so the result must be 01101101.

Important Info In my documentation PDF, it seems there is a little space between XOR and the first xorMask1, so this may be where a ~ or ! or some other negation sign might have been and it could very well be lost due to some weird encoding issues. So I will test the function if it does what the documentation says or what the function declaration says. Hold on to your helmets, will post back with the results (drums please)....

Upvotes: 2

Views: 1513

Answers (7)

Tsaot
Tsaot

Reputation: 36

I've been studying this for a while now, and I think I see what others are not. The XOR AND XOR process is useful for setting multiple bytes without disturbing others. and Example, we have a given byte where we want to set to 1x1x xxx0 where the x's are values we don't care about. Using the XOR AND XOR process, we use the following masks to turn the bits we don't care about on and off. We use the XOR mask to turn bits on and the AND mask to turn bits off, the ones we don't care about for the mask we leave at a default value (0 for an XOR mask [x XOR 0 = x] and 1 for a AND mask [x AND 1 = x]). So given our desired value, our masks look like this:

XOR: 10100000
AND: 01011110

If our mystery bit reads 10010101, the math then follows:

10010101
10100000 XOR
00110101 =
01011110 AND
00010100 =
10100000 XOR
10110100 =

The bits we want on are on, and the bits we want off are off, regardless of their prior state.

This is a nifty bit of logic for managing multiple bits.

EDIT: the last XOR is for toggling. If there is a bit that you know needs to change, but not what to, make it a 1. so lets say we want to toggle the third bit, or masks will be:

XOR1 10100000
AND  01011110
XOR2 10100100

The last interaction would then change to

00010100 =
10100100 XOR
10110000 =

and the third bit is toggled.

Upvotes: 2

Zdeslav Vojkovic
Zdeslav Vojkovic

Reputation: 14581

     00101101 
XOR  01000000
-------------
     01101101
AND  01000000
-------------
     01000000 
XOR  01000000
-------------
     00000000

The documentation is not right. This wouldn't be the first time I see an implementation which totally drifted from the initial implementation, but no one bothered to update the documentation.

I did a quick check so I might be wrong but following would be consistent with the documentation:

newValue = (((currentValue XOR xorMask1) AND ~andMask) XOR xorMask2)

     00101101 
XOR  01100100
-------------
     01001001
AND  10011011
-------------
     00001001 
XOR  01100100
-------------
     01101101

here's the logic table for expression New = Curr XOR Xor1 AND ~And XOR Xor2 where Xor1 == Xor2

CURR: 0 1 0 1   0 1 0 1 
XOR1: 0 0 1 1   0 0 1 1
AND:  0 0 0 0   1 1 1 1 
XOR2: 0 0 1 1   0 0 1 1 
-----------------------
NEW:  0 1 0 1   0 0 1 1 
      ---v---   ---v---
      same as   same as  
      current   xor mask
      where     where
      AND = 0   AND = 1

Upvotes: 4

Graham Borland
Graham Borland

Reputation: 60681

To answer your very simple question, this is how to set a bit:

value |=  0x100;  

This is how to clear a bit:

value &= ~0x100;

In this example 0x100 is 000100000000 in binary, so it's setting/clearing bit 8 (counting from the right).

Others have already pointed out how your code sample just doesn't do what it claims to, so I won't elaborate on that any further.

Upvotes: 1

btstevens89
btstevens89

Reputation: 147

XOR is the logical exclusive or. Which means one or the other, but not both and not neither. Here is the truth table from Wikipedia.

Input
A | B    Output
---------------
0 | 0 |  0
0 | 1 |  1
1 | 0 |  1
1 | 1 |  0

currentValue XOR xorMask1 = 
    00101101 xor 01000000 = 01101101

01010010 AND andMask = 
    01101101 and 01000000 = 01000000

01000000 XOR xorMask2 = 
    01000000 xor 01000000 = 00000000

Upvotes: 0

Kate Gregory
Kate Gregory

Reputation: 18944

Make yourself a truth table and follow a 1 and a 0 through the process.

  • Anything Xor 0 will be left unchanged (1 Xor 0 is 1 ; 0 Xor 0 is 0)
  • Anything Xor 1 will be flipped (1 Xor 1 is 0; 0 Xor 1 is 1)
  • When Anding, everything goes to 0 except where there is a 1 bit in the And mask - those stay unchanged

So your first Xor can only change the second bit from the left, because that's where you have a 1 in the mask. It flips that bit from 0 to 1. The And leaves that bit alone and sets all the others to 0. The second Xor flips your 1 back to 0 and leaves all the others unchanged.

Result: all zeroes like you said.

Is your question what combination of Xor and And will give you the behaviour the documentation says? To turn on just one bit, use a bitwise Or where the mask has just that bit 1 and the others are zero. To turn off just one bit, use a bitwise And where the mask has just that bit 0 and the others are 1. It's laborious and there's a lot of testing, so if you wanted to turn 2 bits on and 3 bits off, this kind of trickery saves a lot of "if"-ing, but if you just want to affect one bit, do it the simple way and ignore this function, which appears to be written not-quite-right.

Upvotes: 0

Hidde
Hidde

Reputation: 11921

p|q|r|s|p^q|(p^q)&r|((p^q)&r)^s|
0|0|0|0| 0 |   0   |     0     |
0|0|0|1| 0 |   0   |     1     |
0|0|1|0| 0 |   0   |     0     |
0|0|1|1| 0 |   0   |     1     |
0|1|0|0| 1 |   0   |     0     |
0|1|0|1| 1 |   0   |     1     |
0|1|1|0| 1 |   1   |     1     |
0|1|1|1| 1 |   1   |     0     |
1|0|0|0| 1 |   0   |     0     |
1|0|0|1| 1 |   0   |     1     |
1|0|1|0| 1 |   1   |     1     |
1|0|1|1| 1 |   1   |     0     |
1|1|0|0| 0 |   0   |     0     |
1|1|0|1| 0 |   0   |     1     |
1|1|1|0| 0 |   0   |     0     |
1|1|1|1| 0 |   0   |     1     |

Check this table for your input values of the bits, to check the output. Change your masks accordingly, to suit your needs of output.

Upvotes: 0

jcern
jcern

Reputation: 7848

The XOR is binary exclusive and will return true only if one or the other bits is set to 1, therefore:

00101101 XOR 01000000 = 01101101
01101101 AND 01000000 = 01000000
01000000 XOR 01000000 = 00000000

Upvotes: 0

Related Questions