advertisement
Some basic information on doing binary math. Used mainly for microcontroller programming or as a primer for more advance cryptology.
I: Introduction
II: Binary Basics and Booleans
III: Operators
I: Introduction
Boolean Algebra and Bitwise Math is extremely low level mathamatics. Not low
level as in "2 + 2 = 4" (or 5 depending on if you've read 1984), but rather
as in the computational level of mathatics the computer does.
I hear a few people voicing the question "why should I learn this?"
For 1, it's intresting and you learn something, but for those of you who like
purpose, for without purpose we would not exist. These operations can be
used for programming microcontollers and are used in encryption, commonly our
friend Mr. WEP.
II: Binary Basics and Booleans
Okay, if you have NO CLUE what binary is go find a tutorial on that, for the
rest of you a quick review.
Binary: Counting to 10 with only 2 fingers (a loose definition)
Base 10 Base 2(binary)
0 0
1 1
2 10
3 11
4 100
5 101
6 110
7 111
8 1000
9 1001
10 1010
Got it? Questions?
Fine, I'll explain a bit(pardon the pun).
Base 10 you count up to ten and carry to the next digit, yes? So base 2 you
count to two and carry, hence no number larger than 1 appears.
Booleans are either a 1 or 0, true or false, on or off, high or
low...depending on how old you are and what you work with. Programmers see
them all the time.
You can think of each bit(0 or 1 in the binary number...ish) as a boolean.
Now...onto the juicy part!
III: Operators
Boolean Algebra and Bitwise Math use a few operators...however its not our
friends(+,-,/,*,%) instead it is &(and), |(or), ^(xor). NOTE: There are
others,but this is enough for now.
Let us explain:
x`
Each operator gives an "output" based on 2 "inputs" or operands(the numbers
we are using).
& returns a 1 if both are 1's and a 0 otherwise
| returns a 1 if either are 1's, both are 1's and a 0 if neither are 1's
^ returns a 1 if both are different and 0 otherwise
These operate bit-by-bit or "digit-by-digit" if you prefer...
examples:
1 & 1 = 1
1 & 0 = 0
0 & 0 = 0
1 | 1 = 1
0 | 1 = 1
0 | 0 = 0
1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 0 = 0
Now for a bit bigger
1010 & 0101 = 00003:40 PM 8/8/2006
1010 | 0101 = 1111
1010 ^ 0101 = 1111
XOR and OR look the same here, yes? I'll show another
1011 | 1110 = 1111
1111 | 1011 = 1111
1011 ^ 1110 = 0101
0101 ^ 1011 = 1110
Okay, so after a few examples and definitions I hope you are completely
confused, not.
"Vi Veri Veniversum Vivus Vici. "
--Samurai

Main:
Posted by 