Scott Cate Weblog 

scott.cate@myKB.com
http://scottcate.myKB.com



Scott Cate KB > ASP.NET Code Samples - Findings > ASP.NET Class Start Over
Search the Knowledge Base
 
Start Search in the Following Category
Date Modified
Friday, April 02, 2004
Using and Understanding BitWise Operations

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

128

64

32

16

 8

 4

 2

1

               
               
               

128

64

32

16

 8

 4

 2

1

               
               
               

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


All rights reserved. All details are the personal opinion of Scott Cate.
All trademarks referenced are the property of their respective owners.
Scott Cate is a lead programmer for www.myKB.com and
owner of The Arizona .NET User Group and an all around nice guy ;)


Knowledge Base Software - myKB.com