Archived

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

LordElectro

Shifting Bits in VB (urgent)

Recommended Posts

LordElectro    122
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 this post


Link to post
Share on other sites
DSurf    122

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 this post


Link to post
Share on other sites
Jumpster    122
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 this post


Link to post
Share on other sites
LordElectro    122
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 this post


Link to post
Share on other sites
Jumpster    122
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 this post


Link to post
Share on other sites
Shannon Barber    1681
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...

Share this post


Link to post
Share on other sites