Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' ***************************************************************************
- ' Routine: w32Shift (Long Integer)
- '
- ' Description: Shifts bits to right or left a specified number of
- ' positions and returns the new value. Bits "falling off"
- ' the edge do not wrap around. Fill bits are zeroes on
- ' the opposite side. Some common languages like C/C++ or
- ' Java have an operator for this job: ">>" or "<<"
- '
- ' When viewing the binary output, remember the first position
- ' is the sign bit. Zero is positive and one is negative.
- '
- ' Verify your results with MS Calculator (CALC.EXE) that comes
- ' with Windows. Use the scientific view.
- '
- ' Parameters: lngValue - Number to be manipulated
- ' lngBitShift - number of shift positions
- ' Positive value = left shift
- ' Negative value = right shift
- '
- ' Returns: Reformatted value
- '
- ' Number Binary
- ' Original: 123456789 00000111010110111100110100010101
- ' Left 5: -344350048 10010100100001100101110101100000
- ' Right 5: 3858024 00000000001110101101111001101000
- '
- ' ===========================================================================
- ' DATE NAME / eMAIL
- ' DESCRIPTION
- ' ----------- --------------------------------------------------------------
- ' 22-May-2006 Dermot Hogan
- ' http://www.bitwisemag.com/2/Bit-Shifting-in-Visual-Basic-6
- ' 28-May-2006 Kenneth Ives kenaso@tx.rr.com
- ' Modified variable names and documented
- ' ***************************************************************************
- Public Function w32Shift(ByVal lngValue As Long, _
- ByVal lngBitShift As Long) As Long
- Dim lngLoop As Long
- Dim lngSignBit As Long
- ' Test amount of bit shifting
- If lngBitShift = 0 Then
- w32Shift = lngValue ' Nothing to do
- ElseIf lngBitShift > 31 Then
- w32Shift = 0 ' Excessive positive bit positions
- ElseIf lngBitShift < -31 Then
- w32Shift = 0 ' Excessive negative bit positions
- ' Positive bit value means shift left
- ElseIf lngBitShift > 0 Then
- ' Shift left by one bit but take in account of
- ' an overflow error from VB. Mask accordingly.
- For lngLoop = 1 To lngBitShift
- ' Calculate sign bit of result
- lngSignBit = lngValue And &HC0000000
- ' Clear Most Significant Bit (MSB), that
- ' would be lost anyway, also clear sign bit
- lngValue = lngValue And &H3FFFFFFF
- lngValue = lngValue * 2
- ' set or clear MSB
- If lngSignBit And &H40000000 Then
- lngValue = lngValue Or &H80000000
- Else
- lngValue = lngValue And &H7FFFFFFF
- End If
- Next lngLoop
- w32Shift = lngValue ' return new value
- ' A negative bit shift value means shift right
- ElseIf lngBitShift < 0 Then
- ' Shift left by one bit but take in account of
- ' an overflow error from VB. Mask accordingly.
- For lngLoop = lngBitShift To -1
- ' Calculate sign bit of result
- lngSignBit = lngValue And &H80000001
- ' Clear Most Significant Bit (MSB), that
- ' would be lost anyway, also clear sign bit
- lngValue = lngValue And &H7FFFFFFF
- lngValue = lngValue \ 2
- ' set or clear the old sign bit
- If lngSignBit And &H80000000 Then
- lngValue = lngValue Or &H40000000
- Else
- lngValue = lngValue And &HBFFFFFFF
- End If
- Next lngLoop
- w32Shift = lngValue ' return new value
- End If
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement