1. Computing

Bitwise Operations in VB.NET

By November 25, 2007

Follow me on:

Working with the 1's and 0's

VB.NET doesn't support bit level operations directly. Framework 1.1 (VB.NET 2003) introduced bit shift operators (<< and >>) for VB.NET, but no general purpose way to manipulate individual bits is available.

This article explains how to do it.

November 25, 2007 at 1:14 pm
(1) PZI says:

The definition (apparently from Microsoft) for arithmetic shift is at least confusing, if not inaccurate.

As far back as UNIVAC I and the IBM 701 there were three kinds of shifts. An arithmetic shift was exacty equivalent to multiplying (or dividing) by some power of the machine’s radix; it always preserved the sign of the number, and copied the sign bit at the left (for a right shift) or inserted zeros at the right (for a left shift).

The second type of shift was a logical shift, which treated the sign as simply another bit (for binary machines) or character (for character machines).

It may be the case that Microsoft has chosen to call all of the shifts arithmetic because it has chosen to make the sign bit a property of the type of object being shifted, rather than a property of the shift; if this is true, then I would expect different results when shifting an Integer compared with shifting a UInteger, but (at least for me) that’s confusing, if true.

Finally, there is the circular shift mentioned in the article. For this type of shift, bits shifted out of one end of the “register” were inserted at the other end, so that the effect was to treat the entire “register” as a loop.

A second sort of comment: At least for me, the hexadecimal representation of binary numbers is frequently more … uh … mnemonic. The article gives, as an example,

1111 1111 1111 1111 1111 1111 1100 1110
0000 0000 0000 0000 0000 0000 0001 1111
0000 0000 0000 0000 0000 0000 0000 1110

but for me, at least, it’s a lot easier to see when written as


but that’s probably because I’ve spent a lot of time working in Hex. While it is true that the article is about “bits”, I suggest that Hex is simply a shorthand for four-bit groups, and might well have been included.

November 25, 2007 at 4:09 pm
(2) visualbasic says:

You wrote, “It may be the case that Microsoft has chosen to call all of the shifts arithmetic because it has chosen to make the sign bit a property of the type of object being shifted, rather than a property of the shift …”

That’s exactly right. “Shift” is a method and methods don’t have properties. Objects do. I think you have probably found a major reason why they did it this way.

As for Hex … Thanks for the example. I work in Hex quite a bit but I have to say … ummmm … the binary still makes more sense to me. In fact, I’m not sure what the rules for logical operations between hex numbers are. Maybe you could enlighten us a ‘bit’ more about this.

November 25, 2007 at 4:57 pm
(3) PZI says:

Well, I suppose one could build a 16-by-16 Karnaugh map, but that’s right up there with using a jackhammer to drive a pushpin into a marshmallow. My reason for using Hex is that it ends up with fewer marks on the “paper”, and hence less possibility of making an error. It’s kind of easy to remember that a hex character is four bits, so if I want to represent the largest positive (four-byte) Integer I write &H7FFFFFFF, and don’t have to think about it, nor count 31 bits.

November 25, 2007 at 5:34 pm
(4) visualbasic says:

That’s exactly the point. My problem is that it’s not visually obvious to me that E And F is E. But it is obvious to me that 1110 And 1111 is 1110. (Without memorizing a 16 by 16 Karnaugh map!)

November 26, 2007 at 2:13 pm
(5) PZI says:

Ah. I offer that we’re looking at the elephant from different angles. I think it’s a lot easier to write &H7FFF than 32767 or 111111111111111. So for notational purposes I like Hex. When I’m actually doing the arithmetic, I agree that bits are easier than a 16×16 Karnaugh map. So my tendency is to use Hex when I’m thinking about bits positioned in a “word” (read Integer or UInteger if you’d like, but I use binary when I’m thinking about manipulating them.

(I also note that I frequently declare an Enum with elements Bit00 = &H1, Bit01 = &H2, and so on, so that I don’t have to think about the details in the middle of writing code [and assigning the value to Bit31 is tricky].)

November 26, 2007 at 3:58 pm
(6) visualbasic says:

I remember the old “elephant” metaphor: The blind men who decided the elephant was different things when they put their hands on it. I always imagined that if I was one of those blind men, instead of saying, “Ah! The elephant is like a rope!” or “Ah! The elephant is like a wall!” I would say, “Ah! The elephant is like a load of wet laundry!”

… anyway …

To tell the truth, when I wrote that article, I made HEAVY use of the Windows calculator and simply switched back and forth between Dec and Bin to do the conversions. Why burn up brain cells thinking when I have a computer?

December 21, 2010 at 3:52 am
(7) Zubair says:

I know this post is really old but anywayz…
Just to correct you.
1111 1111 1111 1111 1111 1111 1100 1110



Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

©2014 About.com. All rights reserved.