You are not logged in.

OlimilO

Intermediate

  • "OlimilO" is male
  • "OlimilO" started this thread

Posts: 277

Date of registration: Jan 18th 2009

Location: Germany

Occupation: software engineer

  • Send private message

1

Tuesday, July 28th 2009, 12:23am

Bit Shifting, Array Initialising

Hi @All,



ever heard about Bit Shifting?

In Visual Basic Classic it is not often used, because there are two things that makes it hard to use:

1. There is neither a special syntax nor a intrinsic function for it

2. by default VB does Integer overflow checks. (dt: Überprüfung auf Ganzzahlüberlauf)



for 2: you could switch this behaviour off:

* click the menuitem "Project"->"Properties of <MyProject>" and

* click the tab "Compile"

* click the button "Advanced Optimizations"

* check: "Remove Integer Overflow Checks"



for 1: In many languages, for bitshifting you use these operators: <<, >> (ShiftLeft, ShiftRight) But in VB6 it is not possible to make Operator-functions. But of course we could imagine a short functionname:
ShL for ShiftLeft, ShR for ShiftRight.

But how to implement it?
the number 14 in 8-Bit-binary view looks like: 00001110
if you shift this pattern 2 bits to the left you get: 00111000
if you shift this pattern 3 bits to the right you get: 00000111
What you have to know:

* Bitshifting a number one bit to the left, is the same as multiplying a number by 2 (2^1=2)

* Bitshifting a number one bit to the right, is the same as dividing a number by 2 (2^1=2)

so the functions can easily be written as:

Jabaco Source

1
2
3
4
5
6
Public Function ShL(value As Integer, shifter As Byte) As Long
   ShL = value * 2 ^ shifter
End Function
Public Function ShR(value As Integer, shifter As Byte) As Long
   ShR = value / 2 ^ shifter
End Function

shifter can not be bigger than 16, 32, or 64 so you could also use an array of Integer, instead of doing always the same "power of 2"-calculation (2 ^ shifter), and this will lead us to a very cool new feature of Jabaco:

Initialising of Arrays with well known constant values

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Private sh() As Integer = (&H1,        &H2,        &H4,        &H8, _
                           &H10,       &H20,       &H40,       &H80, _
                           &H100,      &H200,      &H400,      &H800, _
                           &H1000,     &H2000,     &H4000,     &H8000, _
                           &H10000,    &H20000,    &H40000,    &H80000, _
                           &H100000,   &H200000,   &H400000,   &H800000, _
                           &H1000000,  &H2000000,  &H4000000,  &H8000000, _
                           &H10000000, &H20000000, &H40000000, &H80000000)
 
Public Function ShL(value As Integer, shifter As Byte) As Long
   ShL = value * sh(shifter)
End Function
Public Function ShR(value As Integer, shifter As Byte) As Long
   ShR = value / sh(shifter)
End Function



@Manuel I think it is not a big deal to implement bitshift-operatos for Jabaco isn't it?



Greetings

OlimilO

A1880

Intermediate

  • "A1880" is male

Posts: 500

Date of registration: Jan 1st 2009

Location: Hanover, Germany

Occupation: Software Engineer

Hobbies: Hilbert Curves

  • Send private message

2

Tuesday, July 28th 2009, 2:21pm

Hi,
I tried your code and implemented some testing routines:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
Option Explicit

Public sh(0 To 15) As Integer = ( &H1,        &H2,        &H4,        &H8, _
                                  &H10,       &H20,       &H40,       &H80, _
                                  &H100,      &H200,      &H400,      &H800, _
                                  &H1000,     &H2000,     &H4000,     &H8000, _
                                  &H10000,    &H20000,    &H40000,    &H80000, _
                                  &H100000,   &H200000,   &H400000,   &H800000, _
                                  &H1000000,  &H2000000,  &H4000000,  &H8000000, _
                                  &H10000000, &H20000000, &H40000000, &H80000000)
 

Public Function ShL(value As Integer, shifter As Byte) As Long
   ShL = value * sh(shifter)
End Function

Public Function ShR(value As Integer, shifter As Byte) As Long
   ShR = value / sh(shifter)
End Function

Public Sub shTestAll
   Dim i As Integer
   Dim p As Integer 
   
   p = 1
   For i = 0 To 15
      ' debug.Print i & ": " & i2b(sh(i))
      sh(i) = p
      p = 2 * p
   Next i
   
   shTest "0000000000000001",  1, False, "0000000000000010"
   shTest "0000000000000010",  1, True,  "0000000000000001"
   shTest "1000000000000000",  1, True,  "0100000000000000"
   shTest "1000000000000011",  2, True,  "0010000000000000"
End Sub

Private Sub shTest(value As String, shifter As Byte, bToRight As Boolean, result As String) 
   Dim v As Integer = b2i(value)
   Dim r As Integer = b2i(result)
   Dim ret As Integer 
   
   If bToRight Then
      ret = ShR(v, shifter)
   Else
      ret = ShL(v, shifter)
   End If
   
   If ret <> r Then
      debug.Print value & iif(bToRight, " >> ", " << ") & CStr(shifter) & " = " & i2b(ret) & " <> " & result & " Error!"
   Else
      debug.Print value & iif(bToRight, " >> ", " << ") & CStr(shifter) & " = " & result & " OK!"
   End If
End Sub

Private Function b2i(b As String) As Integer
   Dim ret As Integer = 0
   Dim c As String
   Dim pos As Integer 
   
   For pos = 1 To len(b)
      c = mid(b, pos, 1)
      
      ret = 2 * ret + (c = "1")
   Next pos
   
   b2i = ret
End Function

Private Function i2b(i As Integer) As String
   Dim v As Integer = i
   Dim ret As String = ""
   
   Do While v <> 0
      ret = iif(v Mod 2 = 1, "1", "0") & ret
      
      v = v / 2
   Loop
   
   i2b = iif(ret = "", "0", ret)
End Function


The Jabaco array initialization does not seem to work. All array entries ended up as zero values.
Your routines are OK for positive "value" (i.e. most siginificant bit not set).
For a general purpose framework implementation, some extra error checking would be required.

Cheers!

A1880

OlimilO

Intermediate

  • "OlimilO" is male
  • "OlimilO" started this thread

Posts: 277

Date of registration: Jan 18th 2009

Location: Germany

Occupation: software engineer

  • Send private message

3

Tuesday, July 28th 2009, 2:48pm

Hi A1880,


Yes i made the same mistake in the beginning:

Jabaco Source

1
Public sh(0 To 15) As Integer = (...)


just delete "0 To 15"

it must be:

Jabaco Source

1
Public sh() As Integer = (...)


if the syntax would be not correct, of course there should be a message or red coloured lines etc.

The Array-function in VB also delivers an array with LBound always 0, and the Ubound is defined by the amount of values.



OlimilO

A1880

Intermediate

  • "A1880" is male

Posts: 500

Date of registration: Jan 1st 2009

Location: Hanover, Germany

Occupation: Software Engineer

Hobbies: Hilbert Curves

  • Send private message

4

Tuesday, July 28th 2009, 3:25pm

OK!

What is "kaum macht man's richtig, funktioniert's auch!" in English?

Cheers!

A1880

OlimilO

Intermediate

  • "OlimilO" is male
  • "OlimilO" started this thread

Posts: 277

Date of registration: Jan 18th 2009

Location: Germany

Occupation: software engineer

  • Send private message

5

Tuesday, July 28th 2009, 3:40pm

:)

efgee

Beginner

Posts: 9

Date of registration: Jun 30th 2009

  • Send private message

6

Tuesday, July 28th 2009, 11:37pm

Quoted

kaum macht man es richtig, funktioniert es auch!
translates in yahoo babelfish to:

Quoted

one hardly makes it correctly, functions it also!
which makes me laugh...


I assume the correct meaning should be:

Quoted

once it's done right, it works!
bye
efgee

Rate this thread
WoltLab Burning Board