.
Aun tiene unos HORRORES al tratar con numeros negativos. xP.
La funcion Not es mas facil que nada al tratarse de numeros decimales xP.
Al rato las mejoro un poco ahora me tengo que ir llevo 2 horas de retraso. xS.
Option Explicit
Public Enum tBase
[ Base2 ] = 2
[ Base8 ] = 8
[ Base10 ] = 10 ' // Se obvia como entrada (La dejo solo para verificar cogruencia)
[ Base16 ] = 16
End Enum
Const conlnLong As Long = &H20 ' // Bites de un Long.
Public Function Dec2Bin(ByVal CurVal As Long) As String
Dim lng_lim As Long
Dim lng_index As Long
Dim byt_res As Byte
Dim boo_min As Boolean
If (CurVal < 0) Then
boo_min = True
CurVal = CurVal * -1 - 1
Dec2Bin = String$(conlnLong, "1")
Else
Dec2Bin = String$(conlnLong, "0")
End If
For lng_index = 0 To conlnLong - 1
byt_res = CurVal Mod 2
If (boo_min = True) Then
If (byt_res = 1) Then
byt_res = 0
Else
byt_res = 1
End If
Mid$(Dec2Bin, conlnLong - lng_index, 1) = byt_res
Else
Mid$(Dec2Bin, conlnLong - lng_index, 1) = byt_res
End If
CurVal = CurVal \ 2
If (CurVal = 0) Then Exit For
Next
End Function
' // Hex no afecta a bases inferiores por ello lo dejo.
Public Function Base2Dec(ByRef inval As String, ByRef InBase As tBase) As Long
Dim lng_lenStr&
Dim lng_Pointer&
Dim lng_Potencia&
Dim lng_limit&
lng_lenStr& = Len(inval)
If (lng_lenStr& >= conlnLong) Then
lng_lenStr& = conlnLong
lng_limit& = 2
Else
lng_limit& = InStr(1, inval, "-")
End If
lng_Potencia& = 0
For lng_Pointer& = lng_lenStr& To lng_limit& Step -1
Base2Dec = Base2Dec + CLng("&H" & Mid$(inval, lng_Pointer, 1)) * InBase ^ lng_Potencia&
lng_Potencia& = lng_Potencia& + 1
Next lng_Pointer&
If (Mid$(inval, 1, 1) = "1") Then Base2Dec = Base2Dec * -1
End Function
Public Function AndAlt(Byte1 As Long, Byte2 As Long) As Long
Dim str1 As String
Dim str2 As String
Dim str3 As String * conlnLong
Dim i As Byte
str1 = Dec2Bin(Byte1)
str2 = Dec2Bin(Byte2)
For i = 1 To conlnLong
If (Mid$(str1, i, 1) = "1") Then
If (Mid$(str1, i, 1) = Mid$(str2, i, 1)) Then
Mid$(str3, i, 1) = 1
Else
Mid$(str3, i, 1) = 0
End If
Else
Mid$(str3, i, 1) = 0
End If
Next i
AndAlt = Base2Dec(str3, [ Base2 ])
End Function
Public Function OrAlt(Byte1 As Long, Byte2 As Long) As Long
Dim str1 As String
Dim str2 As String
Dim str3 As String * conlnLong
Dim i As Byte
str1 = Dec2Bin(Byte1)
str2 = Dec2Bin(Byte2)
For i = 1 To conlnLong
If (Mid$(str1, i, 1) = "1") Then
Mid$(str3, i, 1) = 1
ElseIf (Mid$(str2, i, 1) = "1") Then
Mid$(str3, i, 1) = 1
Else
Mid$(str3, i, 1) = 0
End If
Next i
OrAlt = Base2Dec(str3, [ Base2 ])
End Function
Public Function XorAlt(Byte1 As Long, Byte2 As Long) As Long
Dim str1 As String
Dim str2 As String
Dim str3 As String * conlnLong
Dim i As Byte
str1 = Dec2Bin(Byte1)
str2 = Dec2Bin(Byte2)
For i = 1 To conlnLong
If (Mid$(str1, i, 1) = "1") Then
If (Mid$(str2, i, 1) = "0") Then
Mid$(str3, i, 1) = 1
Else
Mid$(str3, i, 1) = 0
End If
Else
If (Mid$(str2, i, 1) = "1") Then
Mid$(str3, i, 1) = 1
Else
Mid$(str3, i, 1) = 0
End If
End If
Next i
XorAlt = Base2Dec(str3, [ Base2 ])
End Function
Public Function NotAlt(Byte1 As Long) As Long
NotAlt = (Byte1 + 1) * -1
End Function
Ejemplo Basico:
Private Sub Form_Load()
MsgBox XorAlt(AndAlt(NotAlt(7451), OrAlt(10, 456)), 1) & vbNewLine & _
CStr((Not 7451) And (10 Or 456) Xor 1)
End Sub
claro esta que los operadores binarios son los mas rapidos y estos ni ninguno va asuperar la velocidad de los originales.
Temibles Lunas!¡.