What does Xor -> And -> Xor do? - c++

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)....

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

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.

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.

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

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.

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.

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

Related

Why is x^0 = x?

I have a very simple question.
Why is a number when XOR'ed with 0 gives the number itself.
Can someone please give the proof using an example.
Lets say I have the number 5
5^0==>
I think the answer should be just the last bit of 5 XOR'ed with 0, but the answer is still 5.
0 is false, and 1 is true.
As per the definition, XOR operation A XOR B is "A or B, but not, A and B". So, since B is false, so the result will be A.
Also, XOR truth table shows that it outputs true whenever the inputs differ:
Input Output
A B XOR Result
0 0 0
0 1 1
1 0 1
1 1 0
As you can see, whatever be the value of A, if it is XORed with 0, the result is the bit itself.
So, as you say:
5 = 101, 0 = 000
When performing XOR operation on the individual bits:
101
000
----
101 = 5.
Hence, the result of X^0 is X itself.
What is there that you did not understand. Please read about XOR
00000101 // = 5
00000000 // = 0
--------
00000101 // = 5
Bit-wise operations operates on set of bits in number - not just on last bit.
So if you perform some bit-wise operation on 32-bit integer, then all 32 bits are affected. So integer 5 is 0.....0000101 (32 bits). If you need just the resulting last bit after xor operation apply binary AND with 1:
<script>
console.log("%i\n",(5^0)&1);
console.log("%i\n",(6^0)&1);
</script>

C++ Bitwise XOR ^ Not Working

The C++ Xor is ^. So if I:
a ^ b
it should do a XOR b
However when the values are 4246661 0 so
4246661 ^ 0
it prints: 4246661 when it really should be 0.
EDIT: Wow I was going off of an online xor calculator which was giving me weird results.. sorry
Am I missing something?
XOR result is 1 if one and only one of the two values is 1, meaning:
0 XOR 0 is 0
0 XOR 1 is 1
1 XOR 0 is 1
1 XOR 1 is 0
So, (4246661 XOR 0), which is (0b10000001100110010000101 XOR 0b0) result is 0b10000001100110010000101...no problem here!
Anything XOR 0 result is Anything
Doing an exclusive or of any number with 0 yields that same number.
bitwise:
1 OR 0 = 1
1 EOR 0 = 1
1 EOR 1 = 0
with numbers :
nbr OR 0 = nbr
nbr EOR 0 = nbr

What's the insight behind A XOR B in bitwise operation?

What I know for A XOR B operation is that the output is 1 if A != B, and 0 if A == B. However, I have no insight about this operation when A and B are not binary.
For example, if A = 1, B = 3, then A XOR B = 2; also, if A = 2, B = 3, then A XOR B = 1. Is there any pattern to the XOR operation for non-binary values?
I have a good understanding of boolean mathematics, so I already understand how XOR works. What I am asking is that how do you, for example, predict the outcome of A XOR B without going through the manual calculation, if A and B are not binaries? Let's pretend that 2 XOR 3 = 1 is not just a mathematical artifact.
Thanks!
Just look at the binary representations of the numbers, and perform the following rules on each bit:
0 XOR 0 = 0
0 XOR 1 = 1
1 XOR 0 = 1
1 XOR 1 = 0
So, 1 XOR 3 is:
1 = 001
3 = 011
XOR = 010 = 2
To convert a (decimal) number to binary, repeatedly divide by two until you get to 0, and then the remainders in reverse order is the binary number:
To convert it back, repeatedly subtract it by the largest power of two that's no bigger than it until you get to 0, having each position in the binary number corresponding to the powers you subtracted by set to 1 (the left-most position corresponds to the 0-th power):
(Images reference)
xor on integers and other data is simply xor of the individual bits:
A: 0|0|0|1 = 1
B: 0|0|1|1 = 3
=======
A^B: 0|0|1|0 = 2
^-- Each column is a single bit xor
When you use bit operations on numbers that are more than one bit, it simply performs the operation on each corresponding bit in the inputs, and that becomes the corresponding bit in the output. So:
A = 1 = 00000001
B = 3 = 00000011
--------
result= 00000010 = 2
A = 2 = 00000010
B = 3 = 00000011
--------
result= 00000001 = 1
The result has a 0 bit wherever the input bits were the same, a 1 bit wherever they were different.
You use the same method when performing AND and OR on integers.

Can I set a sequence of bits without unsetting the previous values?

I've got a sequence of bits, say
0110 [1011] 1111
Let's say I want to set that myddle nybble to 0111 as the new value.
Using a positional masking approach with AND or OR, I seem to have no choice but to first unset the original value to 0000, because if I trying ANDing or ORing against that original value of 1011, I'm not going to come out with the desired result of 0111.
Is there another logical operator I should be using to get the desired effect? Or am I locked into 2 operations every time?
The result after kindly assistance was:
inline void foo(Clazz* parent, const Uint8& material, const bool& x, const bool& y, const bool& z)
{
Uint8 offset = x | (y << 1) | (z << 2); //(0-7)
Uint64 positionMask = 255 << offset * 8; //255 = length of each entry (8 bits), 8 = number of bits per material entry
Uint64 value = material << offset * 8;
parent->childType &= ~positionMask; //flip bits to clear given range.
parent->childType |= value;
}
...I'm sure this will see further improvement, but this is the (semi-)readable version.
If you happen to already know the current values of the bits, you can XOR:
0110 1011 1111
^ 0000 1100 0000
= 0110 0111 1111
(where the 1100 needs to be computed first as the XOR between the current bits and the desired bits).
This is, of course, still 2 operations. The difference is that you could precompute the first XOR in certain circumstances.
Other than this special case, there is no other way. You fundamentally need to represent 3 states: set to 1, set to 0, don't change. You can't do this with a single binary operand.
You may want to use bit fields (and perhaps unions if you want to be able to access your structure as a set of bit fields and as an int at the same time) , something along the lines of:
struct foo
{
unsigned int o1:4;
unsigned int o2:4;
unsigned int o3:4;
};
foo bar;
bar.o2 = 0b0111;
Not sure if it translates into more efficient machine code than your clear/set...
Well, there's an assembly instruction in MMIX for this:
SETL $1, 0x06BF ; 0110 1011 1111
SETL $2, 0x0070 ; 0000 0111 0000
SETL rM, 0x00F0 ; set mask register
MUX $1,$2,$1 ; result is 0110 0111 1111
But in C++ here's what you're probably thinking of as 'unsetting the previous value'.
int S = 0x6BF; // starting value: 0110 1011 1111
int M = 0x0F0; // value mask: 0000 1111 0000
int V = 0x070; // value: 0000 0111 0000
int N = (S&~M) | V; // new value: 0110 0111 1111
But since the intermediate result 0110 0000 1111 from (S&~M) is never stored in a variable anywhere I wouldn't really call it 'unsetting' anything. It's just a bitwise boolean expression. Any boolean expression with the same truth table will work. Here's another one:
N = ((S^V) & M) ^ A; // corresponds to Oli Charlesworth's answer
The related truth tables:
S M V (S& ~M) | V ((S^V) & M) ^ S
0 0 0 0 1 0 0 0 0
* 0 0 1 0 1 1 1 0 0
0 1 0 0 0 0 0 0 0
0 1 1 0 0 1 1 1 1
1 0 0 1 1 1 1 0 1
* 1 0 1 1 1 1 0 0 1
1 1 0 0 0 0 1 1 0
1 1 1 0 0 1 0 0 1
^ ^
|____________________|
The rows marked with '*' don't matter because they won't occur (a bit in V will never be set when the corresponding mask bit is not set). Except for those rows, the truth tables for the expressions are the same.

how to calculate bitwise OR using AND, XOR and shift?

The question seems pretty well formulated
I have a virtual machine which implements only AND, XOR, SHL and SHR, yet I have to do a "OR 0x01" operation.
First of all having a correct bitwise computation for the following two variables is sufficient, because they cover all combinations:
A=0101
B=0011
We want
0101
0011
A or B
0111
for xor we get
0101
0011
A xor B
0110
for and we get
0101
0011
A and B
0001
so if we connect them with an xor we are done.
(A xor B) xor (A and B)
I would just start with
a xor b = ((not a) and b) or (a and (not b))
and unleash some boolean algebra on that until it looks like
a or b = <expression using only and, xor>
Admittedly, this is probably more work to actually do than going the "try every conceivable bit combination" route, but then you did ask for homework solution ideas. :)
The truth table as summarized on Wikipedia here and gasp, basic CS 101 stuff, De Morgan's Law....
AND
0 & 0 0
0 & 1 0
1 & 0 0
1 & 1 1
OR
0 | 0 0
0 | 1 1
1 | 0 1
0 | 0 1
XOR
0 ^ 0 0
0 ^ 1 1
1 ^ 0 1
1 ^ 1 0
A Shift Left involves shifting the bits across from right to left, suppose:
+-+-+-+-+-+-+-+-+
|7|6|5|4|3|2|1|0|
+-+-+-+-+-+-+-+-+
|0|0|0|0|0|1|0|0| = 0x4 hexadecimal or 4 decimal or 100 in binary
+-+-+-+-+-+-+-+-+
Shift Left by 2 places becomes
+-+-+-+-+-+-+-+-+
|7|6|5|4|3|2|1|0|
+-+-+-+-+-+-+-+-+
|0|0|0|1|0|0|0|0| = 0x10 hexadecimal or 16 decimal or 10000 in binary
+-+-+-+-+-+-+-+-+
Shift Right by 1 places becomes
+-+-+-+-+-+-+-+-+
|7|6|5|4|3|2|1|0|
+-+-+-+-+-+-+-+-+
|0|0|0|0|1|0|0|0| = 0x8 hexadecimal or 8 decimal or 1000 in binary
+-+-+-+-+-+-+-+-+
Then it is a matter of combining the bit-wise operations according to the truth table above...
I would just expand DeMorgan's law: A or B = not(not A and not B). You can compute not by XORing with all 1 bits.