Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-11-19
5
Medium Priority
?
487 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 46

Accepted Solution

by:
aikimark earned 2000 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 46

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

722 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