piershughes
asked on
VB Shift-Left / Shift-Right functions on large values
I need to do the 'C' equivilent of << shift left and >> shift right on Hex values like
&HAAAA (1010 1010 1010 1010) 16 bits
I have seen examples on this site like:
Public Function ShiftLeft(Value As Integer, Shift As Integer) As Integer
Dim temp As Long
If Shift < 32 Then
temp = Value * (2 ^ Shift)
Else
temp = 0
End If
ShiftLeft = CInt(temp)
End Function
but this overflows on large values... anyone have a good function for dealing with this?
&HAAAA (1010 1010 1010 1010) 16 bits
I have seen examples on this site like:
Public Function ShiftLeft(Value As Integer, Shift As Integer) As Integer
Dim temp As Long
If Shift < 32 Then
temp = Value * (2 ^ Shift)
Else
temp = 0
End If
ShiftLeft = CInt(temp)
End Function
but this overflows on large values... anyone have a good function for dealing with this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks guys...
Looks like egl1044 repeated the same answer.... so not sure how to award points.
The one who answered correctly first I suspect.
One quick followup question; does the shift Java << function rotate the bits or disgard?
Ie if I do a 1 position leftshift "<< " on "1010 1010 1010 1010"
Does it produce
"0101 0101 0101 0100" Disgarded
or
"0101 0101 0101 0101" Rotated
Thanks
Looks like egl1044 repeated the same answer.... so not sure how to award points.
The one who answered correctly first I suspect.
One quick followup question; does the shift Java << function rotate the bits or disgard?
Ie if I do a 1 position leftshift "<< " on "1010 1010 1010 1010"
Does it produce
"0101 0101 0101 0100" Disgarded
or
"0101 0101 0101 0101" Rotated
Thanks
Usually, ShL and ShR introduce 0 bit values at the trailing end of the shift. However, your application may require bit wrapping, so the basic operations need modifying.
You haven't specified some moderately important pieces of information:
1. What is the masimum amount of bit shifting? (let us know if it differs by direction)
2. How often will this bit shifting take effect?
3. Does your specification of the input (&hAAAA) indicate the true nature/format of the input and output?
4. Is the input and output truely Integer and not Long?
5. You've mentioned overflow as a problem, does sign changing bother you?
6. What is the context for this bit shifting? Is performance a concern? Note: this question is similar to item 2 above.
You haven't specified some moderately important pieces of information:
1. What is the masimum amount of bit shifting? (let us know if it differs by direction)
2. How often will this bit shifting take effect?
3. Does your specification of the input (&hAAAA) indicate the true nature/format of the input and output?
4. Is the input and output truely Integer and not Long?
5. You've mentioned overflow as a problem, does sign changing bother you?
6. What is the context for this bit shifting? Is performance a concern? Note: this question is similar to item 2 above.
Private Type LARGE_INTEGER
Long1 As Long
Long2 As Long
End Type
Private Declare Function ShiftLeft Lib "ntdll" Alias "RtlLargeIntegerShiftLeft"
ByVal Val1 As Long, _
ByVal Val2 As Long, _
ByVal ShiftCount As Long) As LARGE_INTEGER
Private Declare Function ShiftRight Lib "ntdll" Alias "RtlLargeIntegerShiftRight
ByVal Val1 As Long, _
ByVal Val2 As Long, _
ByVal ShiftCount As Long) As LARGE_INTEGER
Private Sub Command1_Click()
Dim a As Long
a = &HAAAA
a = ShiftLeft(a, 0, 4).Long1
Debug.Print "Left: " & a
a = ShiftRight(a, 0, 4).Long1
Debug.Print "Right: " & a
End Sub