| You are here: | About>Computing & Technology>Visual Basic> Using VB.NET> Bitwise Operations in VB.NET |
![]() | Visual Basic |
Bitwise Operations in VB.NETNov 25 2007 How to work with the 1's and 0'sVB.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 In my school, they taught Karnaugh maps instead. The Karnaugh map for all four operations are shown in the illustration below. -------- 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 ...
A standard bit shifting operation would look something like this: Dim StartingValue As Integer = 14913080 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. In decimal: 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 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! |
Las Vegas on a BudgetFind a BargainHotel DealsCheap EatsFree AttractionsEntertainment for Less |
All Topics | Email Article | | | ![]() |
| Advertising Info | News & Events | Work at About | SiteMap | Reprints | Help | Our Story | Be a Guide |
| User Agreement | Ethics Policy | Patent Info. | Privacy Policy | ©2008 About, Inc., A part of The New York Times Company. All rights reserved. |


