#### Archived

This topic is now archived and is closed to further replies.

# Shifting Bits in VB (urgent)

This topic is 6464 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

What are the VB equivelents of the C++ operators << and >> ??? I am stuck using VB for a school project, and I need to optomize a game loop, and all these divisions are sucking CPU like u wouldnt believe it. Plz, anyone that knows how to do left and right shifting of bits in VB.

##### Share on other sites

I have this function that i use:

''SHIFTLEFT ( use negative bitpos value to shift to the right )
Function shift_left(ByVal Value As Integer, ByVal bitpos As Integer) As Integer
shift_left = (Value * (2 ^ bitpos))
End Function

##### Share on other sites
im pretty sure you cant bit shift in VB. for the moment you''re stick with those slow divisions

##### Share on other sites
How about defining a table of floats:

float bitShifts[2][32];
...

bitShifts[0][0] = 1; // Use for ShiftRight...
bitShifts[1][0] = 1; // Use for ShiftLeft...

for (int x=1; x<32; x++)
{
// ShiftRight - Divide by...
bitShifts[0][x] = 1 / (2 ^ x); // ^ here = power of...
// ShiftLeft - Multiply by...
bitShifts[1][x] = (2 ^ x); // ^ here = power of...
}

function ShiftRight( value, bits )
return ( value * bitShifts[0][bits]);

function ShiftLeft( value, bits )
return ( value * bitShifts[1][bits]);

Please keep in mind that I don''t know VB6 so you''ll have to work with this. You should notice a significant difference in speed.

Regards,
Jumpster

##### Share on other sites
Except for Quantom, u guys are missin the point. The idea is to avoid multiplication and division, for the sake of speed. And those are incredibly complex algorithms u gave, when all you have to do is multiply or divide a variable by 2^shiftamount if u wish to shift its bits. but that is slow. i need to shift bits using CPU instruction. anyway to execute assembly in vb?

##### Share on other sites
quote:

Except for Quantom, u guys are missin the point. The idea is to avoid multiplication and division, for the sake of speed. And those are incredibly complex algorithms u gave, when all you have to do is multiply or divide a variable by 2^shiftamount if u wish to shift its bits. but that is slow. i need to shift bits using CPU instruction. anyway to execute assembly in vb?

Yes. Divisions are very slow. That's why I had you doing it JUST once. Well, once for each type of bit shift.

1/(2^8) = 0.00390625

So, to shift the number 534 to the right by 8-bits; multiply it as so: 534 * 0.00390625. Since the division only occurs once, it will be noticeably faster than doing the division every time you want to shift.

Plus, calculating the Power Of two every time also eats up the clock cycles. So the power of's are calc'd once too. It essentially requires the power of - 1 multiplications to achieve it (example: 2 ^ 8 = 7 Multiplies).

As far as using ASM codes in VB - I don't know. I know in old basic you could use the PEEK() and POKE() routines for that functionality but I don't know how it works. You may want to check that out...

Regards,
Jumpster

Edited by - Jumpster on November 4, 2000 2:37:32 PM

##### Share on other sites
heheheh you could make a .dll in C and load it in VB

''course you have to call a function to do it then, and not just embed a few lines of code.

Are you compiling the program? It will run much faster when you do.

Also you could use \ instead of / to do integer division...

1. 1
2. 2
Rutin
21
3. 3
JoeJ
18
4. 4
5. 5

• 14
• 39
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631717
• Total Posts
3001877
×