xisto Community

# Bit Shifting In Vb Shifting bits in Visual Basic

## Recommended Posts

This is for all Visual Basic programmers out there, who want to programm some form of encryption algorithm, or anything else that includes shifting bits left or right, for that matter.

Most of you who have tried to accomplish this, know that Visual Basic doesn't have any function for bit shifting. And bit shifting si very usefull Some of you probably know how to shift bits, but this tutorial is for those who don't know how to do it...

Shifting bits is actualy easy, we use multiplication and division by powers of 2.

For right shifting, we use division:

`NewValue = Value \ (2 ^ BitsToShift)`

And for left shifting, we use multiplication:

`NewValue = Value * (2 ^ BitsToShift)`

Now, that's not all... With Visual Basic, things can get a little trickier

Trouble with VB is that it's Integer (16bit), and Long(32bit) data types are signed, and what that means they can hold both positive and negative values. One bit is used to hold the sign. All remaining bits are used to store actual value.

Integer data type can store 65.535 numbers, but, since it is signed, it can only store numbers from -32.768 upto 32.767.

Take a lok at this (for all you visual types, like me )

`DEC      BIN               HEX     OCT1        0000000000000001  &H1     12        0000000000000010  &H2     232.766   0111111111111110  &H7FFE  77.77632.767   0111111111111111  &H7FFF  77.777-32.767  1000000000000000  &H8000  100.000-32.766  1000000000000001  &H8001  100.001-2       1111111111111110  &HFFFE  177.776-1       1111111111111111  &HFFFF  177.777`

If you take the binary value of -1 (1111111111111111), and convert it to decimal in calculator, you wil get a value of 65.535. Same goes for hexadecimal and octal values... Neat, right

Now, what does this have to do with bit shifting? It has everything to do, because during bit shifting to the right, sign bit is preserved in Visual Basic, and that gives wrong values in the end... Now, this explained, we can go on, and write that shifting function...

SHIFTING BITS RIGHT

In most cases, when people want to shift bits, they want to use unsigned shifting, that is, no negative values. When such a shift occurs, shift bit is not preserved. Now, this requires one extra step in order to work with Visual Basic, and it's signed data types. When value is negative, you need to AND it. Here's the example code

`If Value < 0 Then  NewValue = ((Value And &HFFFE) \ 2) And &H7FFF ' * Shift 1 bit right'  NewValue = ((Value And &HFFFC) \ 4) And &H3FFF ' * Shift 2 bits right'  NewValue = ((Value And &HFFF8) \ 8) And &H1FFF ' * Shift 3 bits right'  NewValue = ((Value And &HFFF0) \ 16) And &HEFFF ' * Shift 4 bits right'  NewValue = ((Value And &HFFE0) \ 32) And &HF7FF ' * Shift 5 bits right'  NewValue = ((Value And &HFFC0) \ 64) And &HFBFF ' * Shift 6 bits right'  NewValue = ((Value And &HFF80) \ 128) And &HFDFF ' * Shift 7 bits right'  NewValue = ((Value And &HFF00) \ 256) And &HFEFF ' * Shift 8 bits rightElse  NewValue = Value \ (2 ^ BitsToShift)End If`

SHIFTING BITS LEFT

Shifting bits to the left, doesn't require any additional operations, it is simply multiplied by the power of 2, and is same for the signed and unsigned numbers:

`NewValue = Value * (2 ^ BitsToShift)`

Some people recommend using Long data type, for Integer Values, in order to use unsigned bit shifting. Others, turning off 'Integer Overflow Check' in 'Advanced Optimisations'... Unsigned right bit shifting can't be tested from the Visual Basic IDE, instead, compile it into a project, turn off already mentined optimisation, and try it that way...

That's all folks

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account