Binary Patterns 1.0
Submitted By:
iwilld0it
Rating:
Not rated (
Rate It)
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "Bin"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
' CONVERTS BIT PATTERN INTO A NUMBER
Function BinToDec(ByVal Bin$) As Long
Dim binLen%, i%, ch$
binLen = Len(Bin)
Bin = Replace(Bin, " ", "")
Bin = StrReverse(Bin)
For i = 1 To binLen
ch = Mid(Bin, i, 1)
If ch = "1" Then
BinToDec = BinToDec + (2 ^ (i - 1))
End If
Next
End Function
' CHECKS TO SEE IF BIT EXIST
Function BinAnd(ByVal op1&, ByVal op2&, Optional ByVal DEPTH% = 16) As String
Dim newVal&
' AND The Two Values
newVal = op1 And op2
BinAnd = DecToBin(newVal, DEPTH)
End Function
' SETS BITS IN A NUMBER
Function BinOR(ByVal op1&, ByVal op2&, Optional ByVal DEPTH% = 16) As String
Dim newVal&
' OR The Two Values
newVal = op1 Or op2
BinOR = DecToBin(newVal, DEPTH)
End Function
Function BinXOR(ByVal op1&, ByVal op2&, Optional ByVal DEPTH% = 16) As String
Dim newVal&
' OR The Two Values
newVal = op1 Xor op2
BinXOR = DecToBin(newVal, DEPTH)
End Function
' INVERTS ALL THE BITS IN A NUMBER
Function BinNot(ByVal op&, Optional DEPTH% = 16) As String
' Return the
BinNot = DecToBin(Not op, DEPTH)
End Function
' DISPLAYS BIT PATTERN OF A WHOLE POSITIVE NUMBER
Function DecToBin(ByVal num&, Optional ByVal DEPTH% = 4) As String
If DEPTH > 30 Then
Err.Raise 6, "Bin", "Bit Depth Overflow"
End If
Dim i%, b&
' Scroll through each bit, starting at the highest bit
For i = (DEPTH - 1) To 0 Step -1
' Get the current bit value (2 to the power of its depth)
' AND the bit value to see if it exist inside the number
b = (2 ^ i) And num
' Set the flag status of the bit
DecToBin = DecToBin & IIf(b > 0, "1", "0")
' Add a space after each nibble
If i Mod 4 = 0 Then
DecToBin = DecToBin & " "
End If
Next
' Remove the trailing space
DecToBin = RTrim(DecToBin)
End Function