logo

 

     
 
Home
Site Map
Search
 
:: Bitwise Courses ::
 
Bitwise Dusty Archives
 
 
 

rss

 
 

ruby in steel

learn aikido in north devon

Learn Aikido in North Devon

 


Section :: visual basic

- Format For Printing...

Bit Shifting in Visual Basic 6

Monday 22 May 2006.
 

In Visual Basic 6 there isn’t a ‘bit shift’ operator. However, you can implement something that does the same job quite easily.

One of the problems with Visual Basic 6 (VB 6) is its lack of operators. An operator is often ‘syntactic sugar’ – that is, there is another way of expressing the operation using a function call. For example, the C && operator (AndAlso in VB .NET) can be simply replaced by two If statements. On the other hand, some operations are fundamental in that they are executed by machine instructions – division and multiplication, say. If these aren’t in the language, there’s not much you can do about it.

But there are other operators which, while not in VB 6, can be reasonably easily simulated. Bit shifting is one such. Bit shifts are the movement of the bits in a word or a byte by a given number of places left or right. They aren’t commonly used in database applications, but they are used in cryptography, image processing and communications. The trick to implementing a bit shift function in VB 6 is to realise that it can be implemented by repeatedly using a multiply or divide by 2. Incidentally, in the processor hardware, it’s the other way round – a multiply or divide is performed by repeated bit shifts.

The key idea is that a shift right by one bit is performed by an integer divide by 2, while a shift left is performed by an integer multiply by 2. But having done that, the question remains of what to do with the bit that you have shifted. There are actually two options: either throw it away or ‘rotate’ it into the top or bottom of the word. Here, I’ll ‘rotate’ the bit.

In practice, you have to watch that you don’t cause an ‘overflow’ – a VB 6 exception resulting from the transition from a positive to a negative quantity when you shift to the left. In the code below, I’ve handled this by ‘masking’ the top two bits of a 32 bit word before multiplying (the code shifts a single bit to the left):

The code example in the associated zip file allows you to input a number (in hexadecimal) and shift it by a number of bits. The example actually uses a 64-bit ‘word’ (just an extension of the above example). If you wish, you can extend it to any number of bits in a fairly obvious manner.

Lastly, is this the most efficient way of doing it? Probably not – if you can divide or multiply by 2, you can do so by any power of 2 and so reduce the number of loop iterations. But the cost of this is increased complexity and hence debugging time.

GIF - 8.1 kb
Just a simple shift left or right. Remember, the input and output are hexadecimal
AddThis Social Bookmark Button

vb source  

Forum

  • Bit Shifting in Visual Basic 6
    17 August 2010, by vx

    heureca! America has been found! This is absolutely fu*king language. It lacks the unsigned integer type, the shifting operator. Why would anybody use it. I remember my 8-bit Atari computer. There was line-numbered basic. I liked it and it was my first contact with programming at 4 years age. At age 6 I played with turtle graphic, then I went forward to use pascal and further C. The VB is good only for teaching childern how to tell the machine what to do, but robot Karel is much better for this purpose.

    • Bit Shifting in Visual Basic 6
      30 July 2011, by YuMERA

      Neseri brate, ti si 2010 rekao da je VB6 sranje a zadnji post je iz 2006 tada vb6 sigurno nije bio sranje...

  • Bit Shifting in Visual Basic 6
    9 May 2010, by Caleb

    Below is C-code for bit shifting operation. I’m going to write this with VB.


    char t_buf[256];

    double startPos = 100 * 1000 DWORD64 pos; pos = (DWORD64)startPos*1000000;

    t_buf[0] = (char)((pos >> 40) & 0x0ff); t_buf[1] = (char)((pos >> 32) & 0x0ff); t_buf[2] = (char)((pos >> 24) & 0x0ff); t_buf[3] = (char)((pos >> 16) & 0x0ff); t_buf[4] = (char)((pos >> 8) & 0x0ff); t_buf[5] = (char)((pos) & 0x0ff);


    How can I convert this code with VB ?

  • Bit Shifting in Visual Basic 6
    1 December 2007, by Saru .ak

    Hi every body, Although it’s too late, I coincide with your topic and read it quickly. Shortly, I have an algorithm (or a way) to rotate left or right bits a number of times without manipulating bit strings or hexadecimal. Honestly, I discovered when I was finding a way to rotate bits (left or right) without converting byte to bits. This because I am a cryptographer and a programmer in VB. for any one wants this way please send me an email : deepcipher0rc@yahoo.com

    sincerly

  • Bit Shifting in Visual Basic 6
    16 October 2006, by Robert Leigh

    Hi there Dermot,

    Thanks for the article. I have done a little bit of C so I understand bit shifting. Also, I understand "casting" in C.

    I have tried your code and for me it doesn’t work. I think that you have put your finger on the problem with the negative number exception. I am trying to write a piece of code that will allow me to represent the serial day -date return expressed in days from January 1 1900. I hadn’t realised the default integer condition with the signed integer giving negative numbers. I can see why now that I have been unable to get this code going to get a serial date integer.

    Googling has returned a surprisingly small amount of search items dealing with this topic. MSDN is hard to read. I can only get a day count as a variant, string or long for some reason. So I have been trying to convert the long to an integer! Obviously, I have to try some other maths. I have been using the datediff function to get the serial day count. Today’s date is 39006. I am shifting, but not removing the trailing Zeros. Val,Int() and CInt are not working for me. Perhaps it is all related to the Integervalue.

    As you seem to have an very good understanding of this topic, I wonder if you could popint me to another source that I might try.

    Regards to you Robert Leigh

    • Bit Shifting in Visual Basic 6
      16 October 2006

      You need to Google for ’Julian Date’ (as here).

      The JD is the standard way of doing this thing. The code is totally incomprehensible, but gives the right answer. It’s used all over the place - astronomy and finance in particular. If you need a 1900 base, just get the JD for 1900 and subtract.

      Hope that helps,

      Dermot

  • Bit Shifting in Visual Basic 6
    28 May 2006, by Kenneth Ives

    I liked your article. However the downloaded code lacked almost any documentation. I got the Shift left and Rotate left to work just fine. The Shift right and Rotate right are not working as planned. Is this to be in a future article? Thank you.

    • Bit-Shifting-in-Visual-Basic-6
      28 May 2006, by Kenneth Ives

      Now I see what you have done. To shift right or rotate right you make the shiftvalue variable a negative value. Thanks for the article. You still need to document your code better.


Home