Boolean math isn't something most people get in high-school, which is unfortunate, since there are only ten types of people on earth (those who know binary, and the others).
I thought I should mention XOR, however (exclusive OR). It's one of the operators, like AND and NOT. It's actually a 'complex machine' in the sense that it's a shorthand for:
true if NOT ((A AND B) AND ((NOT A) AND (NOT B)))
This is because if you said (A OR B) and you wanted it to be true only if A was true *or* B was true but NOT if both were true (the 'exclusive' part).
In programming classes, they often use this operator to demonstrate the (somewhat arcane) task of exchanging the values in two variables without using a third as a holding location (hey, it used to be IMPORTANT to know how to be thrifty with memory).
XOR's usefulness is better demonstrated as a RAID5 array (striped disk set with parity). Here's a (simplified) version of how that works. Let's say you want to store two bytes of data on a three disk RAID5 array.
Byte one is an eight, and is written in binary as 1000
Byte two is a five, and it's written 0101
So you put an eight on the first disk, and a five on the second, and a thirteen on the third as the parity byte.
1000 (DEC 8) XOR 0101 (DEC 5) = 1101 (DEC 13)
1000
0101
-------
1101 (put a one where there's a zero or one, and a zero where they are the same).
So, you lose the second disk, and you need to get the data back? XOR the first disk's byte with the third disk's parity byte.
1000
1101
-------
0101
Put a new second disk in, and it will rebuild it to the original's specifications. Put n disks in the array (it just keeps XORing the new bytes with the parity byte--it doesn't matter how many are in the set--though it does increase the chance of failure as you add elements, which inspired RAID6, that has a duplicate parity section so that losing one disk isn't a near-failure state).
1000
0101
0100 (six)
0010 (two)
1010 (ten)
-------
0001
losing the ten gives:
1000
0101
0100 (six)
0010 (two)
0001 (parity)
-------
1010
For clarity, that last digit (the one's place) is read (down column):
zero XOR one is one (leaving the 1 of 0101 as a one)
one XOR zero is one (changing the 0 of 0100 to a 1, as if it's 0101)
one XOR zero is one (changing the 0 of 0010 to make it as 0011)
one XOR one is zero (making the final result of the one's digit a zero)
Obviously, we are not 'carrying' anything (just in case you wondered). It's just the cumulative XORing of the bits in that set of bytes. And it doesn't really matter how many bytes are in the set. It's FM (fuckin' magic), as
Anyway, that's geek-speak for the day.
And the follow up?
Why did the programmer always confuse Halloween and Xmas?
Because DEC 25 = OCT 31
Leave a comment