VB.NET doesn't support bit level operations directly. Framework 1.1 (VB.NET 2003) introduced bit shift operators (<< and >>), but no general purpose way to manipulate individual bits is available. Bit operations can be very useful. For example, your program might have to interface with another system that requires bit manipulation. But in addition, there are a lot of tricks that can be done using individual bits. This article surveys what can be done with bit manipulation using VB.NET.
You need to understand bitwise operators before anything else. In VB.NET, these are:
Bitwise simply means that the operations can be performed on two binary numbers bit by bit. Microsoft uses truth tables to document bitwise operations. The truth table for And is:
1st Bit 2nd Bit Result
1 1 1
1 0 0
0 1 0
0 0 0
In my school, they taught Karnaugh maps instead. The Karnaugh map for all four operations are shown in the illustration below.
Click Here to display the illustration
Click the Back button on your browser to return
Here's a simple example using the And operation with two, four bit binary numbers:
The result of 1100 And 1010 is 1000.
That's because 1 And 1 is 1 (the first bit) and the rest are 0.
To begin with, let's take a look at the bit operations that are directly supported in VB.NET: bit shifting. Although both left shift and right shift are available, they work the same way so only left shift will be discussed. Bit shifting is most often used in cryptography, image processing and communications.
VB.NET's bit shifting operations ...
- Only work with the four types of integers: Byte, Short, Integer, and Long
- Are arithmetic shifting operations. That means that bits shifted past the end of the result are thrown away, and the bit positions opened up on the other end are set to zero. The alternative is called circular bit shifting and the bits shifted past one end are simply added to the other. VB.NET doesn't support circular bit shifting directly. If you need it, you'll have to code it the old fashioned way: multiplying or dividing by 2.
- Never generate an overflow exception. VB.NET takes care of any possible problems and I'll show you what that means. As noted, you can code your own bit shifting by multiplying or dividing by 2, but if you use the "code your own" approach, you have to test for overflow exceptions that can cause your program to crash.
A standard bit shifting operation would look something like this:
Dim StartingValue As Integer = 14913080
Dim ValueAfterShifting As Integer
ValueAfterShifting = StartingValue << 50
In words, this operation takes the binary value 0000 0000 1110 0011 1000 1110 0011 1000 (14913080 is the equivalent decimal value - notice that it's just a series of 3 0's and 3 1's repeated a few times) and shifts it 50 places left. But since an Integer is only 32 bits long, shifting it 50 places is meaningless. VB.NET solves this problem by masking the shift count with a standard value that matches the data type being used. In this case, ValueAfterShifting is an Integer so the maximum that can be shifted is 32 bits. The standard mask value that works is 31 decimal or 11111.
Masking means that the value, in this case 50, is Anded with the mask. This gives the maximum number of bits that can actually be shifted for that data type.
50 And 31 is 18 - The maximum number of bits that can be shifted
It actually makes more sense in binary. The high order bits that can't be used for the shifting operation are simply stripped away.
110010 And 11111 is 10010
When the code snippet is executed, the result is 954204160 or, in binary, 0011 1000 1110 0000 0000 0000 0000 0000. The 18 bits on the left side of the first binary number are shifted off and the 14 bits on the right side are shifted left.
The other big problem with shifting bits is what happens when the number of places to shift is a negative number. Let's use -50 as the number of bits to shift and see what happens.
ValueAfterShifting = StartingValue << -50
When this code snippet is executed, we get -477233152 or 1110 0011 1000 1110 0000 0000 0000 0000 in binary. The number has been shifted 14 places left. Why 14? VB.NET assumes that the number of places is an unsigned integer and does an And operation with the same mask (31 for Integers).
1111 1111 1111 1111 1111 1111 1100 1110
0000 0000 0000 0000 0000 0000 0001 1111
0000 0000 0000 0000 0000 0000 0000 1110
1110 in binary is 14 decimal. Notice that this is the reverse of shifting a positive 50 places.
On the next page, we move on to some other bit operations, starting with Xor Encryption!