Quantcast

Maximum PC

It is currently Wed Jul 30, 2014 6:57 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: I need to do a bit shift in VB6...
PostPosted: Wed Jul 28, 2004 9:06 am 
INFINITE vCORE
INFINITE vCORE
User avatar

Joined: Mon Jun 14, 2004 6:16 am
Posts: 467
Location: Middletown, DE
I am in need of the code that would be the equivalent of << 8 in VB.Net...

I know VB6 doesn't have this operator built in, and I know multiplying by 256 couldn't possibly be the solution...

If anyone has any tips, I would appreciate it.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jul 28, 2004 9:17 am 
INFINITE vCORE
INFINITE vCORE
User avatar

Joined: Mon Jun 14, 2004 6:16 am
Posts: 467
Location: Middletown, DE
I think I found it...and it looks like it is multiplying by 256...

Function ShiftRight(ByVal lngNumber as Long, ByVal intNumBits as Integer) as Long
'--------------
'BIT SHIFT RIGHT
'--------------

ShiftRight = lngNumber \ 2^intNumBits 'note the integer division op

End Function




Function ShiftLeft(Byval lngNumber as Long, ByVal intNumBits as integer) as Long
'--------------
'BIT SHIFT LEFT
'--------------

ShiftLeft = lngNumber * 2^intNumBits

End Function


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jul 28, 2004 9:23 am 
Professional Dork
Professional Dork
User avatar

Joined: Tue May 25, 2004 12:44 pm
Posts: 1246
Location: Cornhole County
What the hell are you trying to do there? I don't understand this <<8 moon language you're using.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jul 28, 2004 9:29 am 
INFINITE vCORE
INFINITE vCORE
User avatar

Joined: Mon Jun 14, 2004 6:16 am
Posts: 467
Location: Middletown, DE
HK-47 wrote:
What the hell are you trying to do there? I don't understand this <<8 moon language you're using.


I am helping a friend with a project, and we need to read a certain registry key, chop it up, decode it, then display it in a more familiar format...

It involves a bitshift and turning 15 'digits' into 25 'digits'... the original app is coded in VB6...so have to find a way to bit shift in VB6.

Yummy, huh? :)


Top
  Profile  
 
 Post subject: Re: I need to do a bit shift in VB6...
PostPosted: Wed Jul 28, 2004 11:50 am 
Bitchin' Fast 3D Z8000
Bitchin' Fast 3D Z8000
User avatar

Joined: Mon Jun 14, 2004 4:04 pm
Posts: 985
Location: Earth
defkhan1 wrote:
I am in need of the code that would be the equivalent of << 8 in VB.Net...

I know VB6 doesn't have this operator built in, and I know multiplying by 256 couldn't possibly be the solution...

If anyone has any tips, I would appreciate it.



Hope this helps:

Code:
Option Explicit

Private m_lPower2(0 To 31) As Long

Public Function RShift(ByVal lThis As Long, ByVal lBits As Long) As Long
   If (lBits <= 0) Then
      RShift = lThis
   ElseIf (lBits > 63) Then
      ' .. error ...
   ElseIf (lBits > 31) Then
      RShift = 0
   Else
      If (lThis And m_lPower2(31 - lBits)) = m_lPower2(31 - lBits) Then
         RShift = (lThis And (m_lPower2(31 - lBits) - 1)) * m_lPower2(lBits) Or m_lPower2(31)
      Else
         RShift = (lThis And (m_lPower2(31 - lBits) - 1)) * m_lPower2(lBits)
      End If
   End If
End Function

Public Function LShift(ByVal lThis As Long, ByVal lBits As Long) As Long
   If (lBits <= 0) Then
      LShift = lThis
   ElseIf (lBits > 63) Then
      ' ... error ...
   ElseIf (lBits > 31) Then
      LShift = 0
   Else
      If (lThis And m_lPower2(31)) = m_lPower2(31) Then
         LShift = (lThis And &H7FFFFFFF) \ m_lPower2(lBits) Or m_lPower2(31 - lBits)
      Else
         LShift = lThis \ m_lPower2(lBits)
      End If
   End If
End Function

Public Sub Init()
   m_lPower2(0) = &H1&
   m_lPower2(1) = &H2&
   m_lPower2(2) = &H4&
   m_lPower2(3) = &H8&
   m_lPower2(4) = &H10&
   m_lPower2(5) = &H20&
   m_lPower2(6) = &H40&
   m_lPower2(7) = &H80&
   m_lPower2(8) = &H100&
   m_lPower2(9) = &H200&
   m_lPower2(10) = &H400&
   m_lPower2(11) = &H800&
   m_lPower2(12) = &H1000&
   m_lPower2(13) = &H2000&
   m_lPower2(14) = &H4000&
   m_lPower2(15) = &H8000&
   m_lPower2(16) = &H10000
   m_lPower2(17) = &H20000
   m_lPower2(18) = &H40000
   m_lPower2(19) = &H80000
   m_lPower2(20) = &H100000
   m_lPower2(21) = &H200000
   m_lPower2(22) = &H400000
   m_lPower2(23) = &H800000
   m_lPower2(24) = &H1000000
   m_lPower2(25) = &H2000000
   m_lPower2(26) = &H4000000
   m_lPower2(27) = &H8000000
   m_lPower2(28) = &H10000000
   m_lPower2(29) = &H20000000
   m_lPower2(30) = &H40000000
   m_lPower2(31) = &H80000000
End Sub


Souce: http://www.vbaccelerator.com/home/VB/Tips/Implementing_Unsigned_Right_and_Left_Shift_Operators/article.asp


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jul 28, 2004 11:56 am 
Bitchin' Fast 3D Z8000
Bitchin' Fast 3D Z8000
User avatar

Joined: Mon Jun 14, 2004 4:04 pm
Posts: 985
Location: Earth
HK-47 wrote:
What the hell are you trying to do there? I don't understand this <<8 moon language you're using.


In C/C++, you can shift the bits of a value type. In an 8-bit system, you may have something like:

00000000 = 0 (in integer)

If I were to apply << 1 or left-shift 1 bit, then I would get:

0000001 = 1
If I were to apply another left shift, then would get:

0000010 = 2

In essence, you're shifting the bits of a variable by some number (typicall less than or equal to the bit size of the type you're dealing with) to get another number, when you bit shift, you're in essence incrementing your variable by powers of 2.

Right shifts are the same:

10000000 = 128

If I right-shift by 1, >> 1

01000000 = 64

So now, with a right shift, you're decrementing by powers of 2.

Anyhow, most C/C++, Java, and C# programmers know this by heart.[/code]


Top
  Profile  
 
 Post subject: Re: I need to do a bit shift in VB6...
PostPosted: Wed Jul 28, 2004 4:47 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
defkhan1 wrote:
I am in need of the code that would be the equivalent of << 8 in VB.Net...

I know VB6 doesn't have this operator built in, and I know multiplying by 256 couldn't possibly be the solution...

If anyone has any tips, I would appreciate it.

Multiplying by 256 is correct.* The reason becomes pretty obvious with an example....

Say we want to bitshift the binary number 001001, which is 9 in decimal.
1*2^3 + 1*2^0 = 8 + 1 = 9

A single shift to the left is 010010, which is 18 in decimal.
1*2^4 + 1*2^1 = 16 + 2 = 18

A second shift gives us 100100, which is 1*2^5 + 1*2^2 = 25 + 4 = 29.

As you can see, the a simple left or right shift is in fact just multiplying or dividing by 2, so <<8 is in fact the equivalent of number*2^8.

* A word of caution though, there are several different types of shifts and you need to be clear about which kind you need..... 1001 <2 = 100100 or 0101 or 0100 w/ the overflow bit set, etc depending on what kind of shift. I don't know VB, but I imagine the <<8 is a normal shift and not a rotate, still you might want to look into what happens with an overflow.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jul 28, 2004 4:51 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
defkhan1 wrote:
HK-47 wrote:
What the hell are you trying to do there? I don't understand this <<8 moon language you're using.


I am helping a friend with a project, and we need to read a certain registry key, chop it up, decode it, then display it in a more familiar format...

It involves a bitshift and turning 15 'digits' into 25 'digits'... the original app is coded in VB6...so have to find a way to bit shift in VB6.

Yummy, huh? :)

Are you turning 15 digits into 25 digits or 15 bits into 25 bits? If you doing the former, you're going to want to multiply by 10^10, but you're also probably going to overflow the var. If the latter, instead of bit shifting, you could multiply by 2^10 or convert to string and append "0000000000" convert back, etc.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Jul 29, 2004 5:26 am 
INFINITE vCORE
INFINITE vCORE
User avatar

Joined: Mon Jun 14, 2004 6:16 am
Posts: 467
Location: Middletown, DE
We already have the code to "extract" the 25 characters from the 15 Byte values, we just needed to do a simple shift 8 bits left on each byte before running it through the 'extraction' module...

Anyone guessed what we're doing yet?

Hint:
reg.ReadRemoteValue("MACHINENAME", HKEY_LOCAL_MACHINE, "XXXmagic keyXXXXX", "SOFTWARE\Microsoft\Windows NT\CurrentVersion")


Top
  Profile  
 
 Post subject:
PostPosted: Sat Jul 31, 2004 8:14 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Not quite sure what you're up to... give us an example. Did that 2^num_shifts thing work out for you?


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group