Click to See Complete Forum and Search --> : Bit Shift Left


Jerome Hovington
March 21st, 2002, 12:46 PM
hi everyone,
I look at new feature of VB .Net and don't found any function that do a bitshitleft on an integer. I already have a function that do that, but it's a lot of job : check sign bit, multiply by 2 factor, ... In C++ you have the operator <<. It's so simple.
Is there a undocumented function that do bitshift. If not, is there a way to include a piece of C++ code in VB (Like Assembler in C++).

Thanks

Iouri
March 22nd, 2002, 11:49 AM
'Enumeration of bit-shifting
public Enum dcShiftDirection
Left = -1
Right = 0
End Enum

Shifting bits to the left acts as a multiplier and to the right divides.

public Function Shift(byval lValue as Long, byval lNumberOfBitsToShift as Long, byval lDirectionToShift as dcShiftDirection) as Long
Const ksCallname as string = "Shift"

on error GoTo Procedure_Error
Dim LShift as Long
If lDirectionToShift then
'shift left
LShift = lValue * (2 ^ lNumberOfBitsToShift)
else
'shift right
LShift = lValue \ (2 ^ lNumberOfBitsToShift)
End If
Procedure_Exit:
Shift = LShift
Exit Function
Procedure_Error:
Err.Raise Err.Number, ksCallname, Err.Description, Err.HelpFile, Err.HelpContext
resume Procedure_Exit
End Function

'===============================================================================
public Function LShift(byval lValue as Long, byval lNumberOfBitsToShift as Long) as Long
Const ksCallname as string = "LShift"
on error GoTo Procedure_Error
LShift = Shift(lValue, lNumberOfBitsToShift, Left)
Procedure_Exit:
Exit Function
Procedure_Error:
Err.Raise Err.Number, ksCallname, Err.Description, Err.HelpFile, Err.HelpContext
resume Procedure_Exit
End Function

'================================================================================
public Function RShift(byval lValue as Long, byval lNumberOfBitsToShift as Long) as Long
Const ksCallname as string = "RShift"
on error GoTo Procedure_Error
RShift = Shift(lValue, lNumberOfBitsToShift, Right)
Procedure_Exit:
Exit Function
Procedure_Error:
Err.Raise Err.Number, ksCallname, Err.Description, Err.HelpFile, Err.HelpContext
resume Procedure_Exit
End Function




Iouri Boutchkine
iouri@hotsheet.com

Jerome Hovington
March 22nd, 2002, 03:01 PM
Thanks Iouri,
The problem with this code, is the Sign bit. If you have the bit 30 (Starting at 0) set to true, what's happend when you bitshift left: Overflow. Also, when you have the bit 31 set to true, does the bit will follow when you will bitshiftright?