I saw AND's, OR's, and XOR's used on numbers in a C++ tutorial I was hoping to port to DBP the other day and didn't really have a clue what they did. So I thought I would share my new knowledge with those unfamiliar with bitwise operators.
First off, binary number system. If you have a basic understanding of how it works, skip this paragraph. If not, read on. Our number system has 10 digits, 0-9. When we hit 09, we increment by one to 10, then 11, 12, 13...up to 19, then to 20, etc. In binary, there are two digits:
0 and
1. 0 in binary would be 0. 1 would be 1. But 2
WOULD NOT be 2, because the binary system does not make use of the digit 2. We would go to 10. Then 11 for 3. Get it? If not, look for more in-depth tutorials on google. This is the best I can explain it for now.
There are three bitwise operators I will cover right now, AND, OR, and XOR. The purpose of the AND command is to put 0's into a binary number. For example, we have the binary number 000110 and we want to make sure the middle 4 digits become 0's. We will AND the number. Bitwise operators take two digits of the same place from two seperate binary numbers, compares them, then makes a new number from the results. When ANDing, the second number is the major force. If the digit in the second number is a 1, the resulting number for that digit is the same as what the digit was in the first number. If it is a 0, then the resulting digit becomes a 0. The second binary number can be thought of as a filter, with each digit being a slot. 1 slots are open, letting the first number come through, while 0 slots block data off, ending in a 0.
Example in DBP: 001100 && 100100 = 000100
OR's do the opposite of AND's. 1 slots result in 1's, while 0 slots retain original data. This is because in an OR, the resulting data is
NUMBER 1 OR NUMBER 2, so if either is a 1 then it results in a 1. Regardless of the original number, the second number makes it a 1 everytime. 0's result in 1 or 0 depending on the original data however.
Example in DBP: 001100 II 100100 = 101100
XOR's are used to invert data. XOR is similar to OR, but it is checking to see if one is a 1 and the other is a 0. 0's retain original data once again and 1's reverse, or invert the data.
Example in DBP: 001100 ~~ 100100 = 101000
Hopefully that is a good intro, I wrote this in 15 minutes during my lunch period. I will be back tonight to fix up stuff and add more! Comments much appreciated.
Matt
Why make sense when you could make brownies?