Bitwise Operations are extremely fast, accurate, and amazingly useful. Unfortunately the term "bitwise" is not mainstream, and therefor seems to have an intimidating connotation.
Remember the old days, when you had a 9600 Baud modem, and you had control over certain options by flipping "Dip Switches" on the back of the modem? You may have not known it, but you were using bitwise security.
To understand Bitwise operations, you must know we'll be comparing bits. So first, What's a bit? A bit is a zero or one. Plain and simple, nothing more, nothing less. The value of a bit will always have the value 0 or 1.
So lets look at the binary system that uses bits to represent a number system.
(The following is blank for white board presentations.)
The integer 8 is represented in binary as 1000.
| 8 | 4 | 2 | 1 |
------------------
| 1 | 0 | 0 | 0 |
The integer 9 is represented in binary as 1001.
| 8 | 4 | 2 | 1 |
------------------
| 1 | 0 | 0 | 1 |
The integer 10 is represented in binary as 1010.
| 8 | 4 | 2 | 1 |
------------------
| 1 | 0 | 1 | 0 |
The integer 13 is represented in binary as 1101.
| 8 | 4 | 2 | 1 |
------------------
| 1 | 1 | 0 | 0 |
Okay, enough examples.
Going back to the modem example, image a bank of four bit switched, that can be flipped up or down. This gives us 16 combinations. 0000 - 1111. These values represented in integer values would be 0 - 15.
Okay, so now we have a general understanding of binary. What does this have to do with bitwise operations while programming in C# or VB.NET?
Well imagine that our program needs to read the dip switch settings from our modem above. We basically have 4 options that can be turned on or off, right? Lets assume that we're reading the switches from left to right, the options are ....
- Show Power Light (Left most switch) [Value 8]
- Show Outbound Bandwidth Light [Value = 4]
- Show Inbound Bandwidth Light [Value=2]
- Show Any Activity Light (Right most switch) [Value=1]
(I've ran out of time today, and can't finish the article. Hopefully I'll have tim in the future to come back and finish this article.)