Solved

VB Shift-Left  / Shift-Right functions on large values

Posted on 2006-11-19
5
459 Views
Last Modified: 2008-02-01
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?
0
Comment
Question by:piershughes
5 Comments
 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
ID: 17976399
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 17976465
Option Explicit

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
0
 

Author Comment

by:piershughes
ID: 17976973
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
0
 
LVL 5

Expert Comment

by:Alkali_Guy
ID: 17977227
I think it disregards the extra bits.

http://en.wikipedia.org/wiki/Bitwise_operation
0
 
LVL 45

Expert Comment

by:aikimark
ID: 17979144
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.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now