Shifting Bits in VB (urgent)

Started by
5 comments, last by LordElectro 23 years, 5 months ago
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.
BetaShare - Run Your Beta Right!
Advertisement

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

im pretty sure you cant bit shift in VB. for the moment you''re stick with those slow divisions
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

Regards,JumpsterSemper Fi
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?
BetaShare - Run Your Beta Right!
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
Regards,JumpsterSemper Fi
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...

- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement