Autor Tema: [SOURCE] Reemplazo de operadores binarios.  (Leído 2483 veces)

0 Usuarios y 1 Visitante están viendo este tema.

79137913

  • Megabyte
  • ***
  • Mensajes: 185
  • Reputación: +21/-4
  • 4 Esquinas
    • Ver Perfil
    • Eco.Resumen Resumenes Cs. Economicas
[SOURCE] Reemplazo de operadores binarios.
« en: Octubre 24, 2011, 10:54:00 am »
HOLA!!!

Código: [Seleccionar]
AndAlt(Byte1 As Long, Byte2 As Long) as Long
OrAlt(Byte1 As Long, Byte2 As Long) as Long
XorAlt(Byte1 As Long, Byte2 As Long) as Long

Creo que mucho no hay que explicar las funciones deben devolver lo mismo que los operadores binarios convencionales.

P.D2:Para los que no saben como funcionan los operadores binarios:
Código: [Seleccionar]
Primero los valores se convierten a binario y luego se hace esto:

And: Solo si se comparte el mismo bit en ambos numeros.
Valor 1 = 0 0 1 0 1 0 0 0
Valor 2 = 1 0 1 1 1 0 1 1
        -----------------
Result  = 0 0 1 0 1 0 0 0

Or : Solo si uno tiene un bit "1".
Valor 1 = 0 0 1 0 1 0 0 0
Valor 2 = 1 0 1 1 1 0 1 1
        -----------------
Result  = 1 0 1 1 1 0 1 1

Xor : Solo si uno tiene un bit "1" y el otro "0".
Valor 1 = 0 0 1 0 1 0 0 0
Valor 2 = 1 0 1 1 1 0 1 1
        -----------------
Result  = 1 0 0 1 0 0 1 1

CODIGO:

Aca les dejo mi solucion:

Las funciones son independientes...

Tengo problemas con el And y Or Negativos :P.

Utilizo 3 Array de Flags(bool) Para guardar los numeros y el resultado en binario.

Soporte para tipos de datos Dec, Hex, Oct y Bin.

Donde dice Byte1 y Byte2 van las condiciones ;)

P. Ej: If AndAlt(12 = 1, 3=3) then ' Es lo mismo que : If 12=1 and 3=3 then

Código: (vb) [Seleccionar]
Private Function AndAlt(Byte1 As Long, Byte2 As Long) As Long
Dim bit1() As Boolean
Dim bit2() As Boolean
Dim bit3() As Boolean
Dim CT     As Long
Dim Tam    As Long
Dim b1     As Long
Dim b2     As Long
b1 = Byte1
b2 = Byte2
    Do
        ReDim Preserve bit1(CT)
        If b1 = 1 Then ReDim Preserve bit1(CT): bit1(CT) = True: Exit Do
        If b1 = 0 Then ReDim Preserve bit1(CT): Exit Do
        bit1(CT) = CBool(b1 Mod 2)
        b1 = Fix(b1 / 2)
        CT = CT + 1
    Loop
    CT = 0
    Do
        If b2 = 1 Then ReDim Preserve bit2(CT): bit2(CT) = True: Exit Do
        If b2 = 0 Then ReDim Preserve bit2(CT): Exit Do
        ReDim Preserve bit2(CT)
        bit2(CT) = CBool(b2 Mod 2)
        b2 = Fix(b2 / 2)
        CT = CT + 1
    Loop
    If UBound(bit1) > UBound(bit2) Then ReDim Preserve bit2(UBound(bit1))
    If UBound(bit1) < UBound(bit2) Then ReDim Preserve bit1(UBound(bit2))
    Tam = UBound(bit1)
    ReDim bit3(Tam)
    For X = 0 To Tam
        If bit1(X) Then If bit2(X) Then bit3(X) = True
    Next
    For X = 0 To Tam
        If bit3(X) Then AndAlt = AndAlt + 2 ^ (X)
    Next

End Function

Private Function OrAlt(Byte1 As Long, Byte2 As Long) As Long
Dim bit1() As Boolean
Dim bit2() As Boolean
Dim bit3() As Boolean
Dim CT     As Long
Dim Tam    As Long
Dim b1     As Long
Dim b2     As Long
b1 = Byte1
b2 = Byte2
    Do
        ReDim Preserve bit1(CT)
        If b1 = 1 Then ReDim Preserve bit1(CT): bit1(CT) = True: Exit Do
        If b1 = 0 Then ReDim Preserve bit1(CT): Exit Do
        bit1(CT) = CBool(b1 Mod 2)
        b1 = Fix(b1 / 2)
        CT = CT + 1
    Loop
    CT = 0
    Do
        If b2 = 1 Then ReDim Preserve bit2(CT): bit2(CT) = True: Exit Do
        If b2 = 0 Then ReDim Preserve bit2(CT): Exit Do
        ReDim Preserve bit2(CT)
        bit2(CT) = CBool(b2 Mod 2)
        b2 = Fix(b2 / 2)
        CT = CT + 1
    Loop
    If UBound(bit1) > UBound(bit2) Then ReDim Preserve bit2(UBound(bit1))
    If UBound(bit1) < UBound(bit2) Then ReDim Preserve bit1(UBound(bit2))
    Tam = UBound(bit1)
    ReDim bit3(Tam)
    For X = 0 To Tam
        If bit1(X) Then bit3(X) = True
        If bit2(X) Then bit3(X) = True
    Next
    For X = 0 To Tam
        If bit3(X) Then OrAlt = OrAlt + 2 ^ (X)
    Next

End Function

Private Function XorAlt(Byte1 As Long, Byte2 As Long) As Long
Dim bit1() As Boolean
Dim bit2() As Boolean
Dim bit3() As Boolean
Dim CT     As Long
Dim Tam    As Long
Dim b1     As Long
Dim b2     As Long
b1 = Byte1
b2 = Byte2
    Do
        ReDim Preserve bit1(CT)
        If b1 = 1 Then ReDim Preserve bit1(CT): bit1(CT) = True: Exit Do
        If b1 = 0 Then ReDim Preserve bit1(CT): Exit Do
        bit1(CT) = CBool(b1 Mod 2)
        b1 = Fix(b1 / 2)
        CT = CT + 1
    Loop
    CT = 0
    Do
        If b2 = 1 Then ReDim Preserve bit2(CT): bit2(CT) = True: Exit Do
        If b2 = 0 Then ReDim Preserve bit2(CT): Exit Do
        ReDim Preserve bit2(CT)
        bit2(CT) = CBool(b2 Mod 2)
        b2 = Fix(b2 / 2)
        CT = CT + 1
    Loop
    If UBound(bit1) > UBound(bit2) Then ReDim Preserve bit2(UBound(bit1))
    If UBound(bit1) < UBound(bit2) Then ReDim Preserve bit1(UBound(bit2))
    Tam = UBound(bit1)
    ReDim bit3(Tam)
    For X = 0 To Tam
        If bit1(X) Then If bit2(X) = False Then bit3(X) = True
        If bit2(X) Then If bit1(X) = False Then bit3(X) = True
    Next
    For X = 0 To Tam
        If bit3(X) Then XorAlt = XorAlt + 2 ^ (X)
    Next

End Function

Private Function NotAlt(Byte1 As Long) As Long
    NotAlt = -(Byte1 + 1)
End Function

GRACIAS POR LEER!!!
"Como no se puede igualar a Dios, ya he decidido que hacer, ¡SUPERARLO!"
"La peor de las ignorancias es no saber corregirlas"

 79137913                          *Shadow Scouts Team*                                                          Resumenes Cs.Economicas