Solved

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

Posted on 2006-11-19
5
474 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

733 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