Advertisement
Guest User

VB shiftbit

a guest
May 31st, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' ***************************************************************************
  2. ' Routine:       w32Shift (Long Integer)
  3. '
  4. ' Description:   Shifts bits to right or left a specified number of
  5. '                positions and returns the new value.  Bits "falling off"
  6. '                the edge do not wrap around.  Fill bits are zeroes on
  7. '                the opposite side.  Some common languages like C/C++ or
  8. '                Java have an operator for this job:  ">>" or "<<"
  9. '
  10. '                When viewing the binary output, remember the first position
  11. '                is the sign bit.  Zero is positive and one is negative.
  12. '
  13. '                Verify your results with MS Calculator (CALC.EXE) that comes
  14. '                with Windows.  Use the scientific view.
  15. '
  16. ' Parameters:    lngValue    - Number to be manipulated
  17. '                lngBitShift - number of shift positions
  18. '                              Positive value = left shift
  19. '                              Negative value = right shift
  20. '
  21. ' Returns:       Reformatted value
  22. '
  23. '                   Number                Binary
  24. ' Original:       123456789   00000111010110111100110100010101
  25. '   Left 5:      -344350048   10010100100001100101110101100000
  26. '  Right 5:       3858024     00000000001110101101111001101000
  27. '
  28. ' ===========================================================================
  29. '    DATE      NAME / eMAIL
  30. '              DESCRIPTION
  31. ' -----------  --------------------------------------------------------------
  32. ' 22-May-2006  Dermot Hogan
  33. '              http://www.bitwisemag.com/2/Bit-Shifting-in-Visual-Basic-6
  34. ' 28-May-2006  Kenneth Ives  kenaso@tx.rr.com
  35. '              Modified variable names and documented
  36. ' ***************************************************************************
  37. Public Function w32Shift(ByVal lngValue As Long, _
  38.                          ByVal lngBitShift As Long) As Long
  39.  
  40.     Dim lngLoop    As Long
  41.     Dim lngSignBit As Long
  42.  
  43.     ' Test amount of bit shifting
  44.    If lngBitShift = 0 Then
  45.         w32Shift = lngValue   ' Nothing to do
  46.    ElseIf lngBitShift > 31 Then
  47.         w32Shift = 0          ' Excessive positive bit positions
  48.    ElseIf lngBitShift < -31 Then
  49.         w32Shift = 0          ' Excessive negative bit positions
  50.  
  51.     ' Positive bit value means shift left
  52.    ElseIf lngBitShift > 0 Then
  53.                
  54.         ' Shift left by one bit but take in account of
  55.        ' an overflow error from VB. Mask accordingly.
  56.        For lngLoop = 1 To lngBitShift
  57.            
  58.             ' Calculate sign bit of result
  59.            lngSignBit = lngValue And &HC0000000
  60.            
  61.             ' Clear Most Significant Bit (MSB), that
  62.            ' would be lost anyway, also clear sign bit
  63.            lngValue = lngValue And &H3FFFFFFF
  64.             lngValue = lngValue * 2
  65.            
  66.             ' set or clear MSB
  67.            If lngSignBit And &H40000000 Then
  68.                 lngValue = lngValue Or &H80000000
  69.             Else
  70.                 lngValue = lngValue And &H7FFFFFFF
  71.             End If
  72.        
  73.         Next lngLoop
  74.        
  75.         w32Shift = lngValue   ' return new value
  76.  
  77.     ' A negative bit shift value means shift right
  78.    ElseIf lngBitShift < 0 Then
  79.                
  80.         ' Shift left by one bit but take in account of
  81.        ' an overflow error from VB. Mask accordingly.
  82.        For lngLoop = lngBitShift To -1
  83.            
  84.             ' Calculate sign bit of result
  85.            lngSignBit = lngValue And &H80000001
  86.            
  87.             ' Clear Most Significant Bit (MSB), that
  88.            ' would be lost anyway, also clear sign bit
  89.            lngValue = lngValue And &H7FFFFFFF
  90.             lngValue = lngValue \ 2
  91.            
  92.             ' set or clear the old sign bit
  93.            If lngSignBit And &H80000000 Then
  94.                 lngValue = lngValue Or &H40000000
  95.             Else
  96.                 lngValue = lngValue And &HBFFFFFFF
  97.             End If
  98.        
  99.         Next lngLoop
  100.    
  101.         w32Shift = lngValue   ' return new value
  102.  
  103.     End If
  104.  
  105. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement