Solved

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

Posted on 2006-11-19
5
481 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

Industry Leaders: 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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

734 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