forked from Vitosh/VBA_personal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary.vb
57 lines (39 loc) · 1.18 KB
/
Binary.vb
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
Option Explicit
Option Private Module
Public Sub TestMe()
Dim arrProducts As Variant
Dim lngCounter As Long
Dim lngValue As Long
Dim strBinary As String
Dim lngNumber As Long
arrProducts = Array("AAA", "BBB", "CCC", "DDD", "EEE", "FFF", "GGG")
'1, 2, 4, 8, 16, 32, 64
lngNumber = 65 '1+2+8+16
strBinary = StrReverse(LngToBinary(lngNumber))
For lngCounter = 1 To Len(strBinary)
lngValue = Mid(strBinary, lngCounter, 1)
If lngValue Then
Debug.Print arrProducts(lngCounter - 1)
End If
Next lngCounter
End Sub
Function LngToBinary(ByVal n As Long) As String
Dim k As Long
LngToBinary = vbNullString
If n < -2 ^ 15 Then
LngToBinary = "0"
n = n + 2 ^ 16
k = 2 ^ 14
ElseIf n < 0 Then
LngToBinary = "1"
n = n + 2 ^ 15
k = 2 ^ 14
Else
k = 2 ^ 15
End If
Do While k >= 1
LngToBinary = LngToBinary & Fix(n / k)
n = n - k * Fix(n / k)
k = k / 2
Loop
End Function