Foro de elhacker.net

Programación => Programación Visual Basic => Mensaje iniciado por: 79137913 en 7 Abril 2011, 17:19 pm



Título: [RETO] Reemplazo de Operadores Binarios.
Publicado por: 79137913 en 7 Abril 2011, 17:19 pm
HOLA!!!

Me parece que va a estar bueno, el reto consiste en crear 3 funciones:
(Usa Long por la rapidez y para soportar numeros grandes)

Código:
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.D: Me habia olvidado... NotAlt(Byte1 As Long)
P.D2: Creo que es obvio, no se pueden usar los operadores en las funciones.
P.D3:Para los que no saben como funcionan los operadores binarios:
Código:
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

GRACIAS POR LEER!!!


Título: Re: [RETO] Reemplazo de Operadores Binarios.
Publicado por: BlackZeroX en 7 Abril 2011, 19:56 pm
.
interesante, regresando le hechare mano de obra.

Dulces Lunas!ˇ.


Título: Re: [RETO] Reemplazo de Operadores Binarios.
Publicado por: BlackZeroX en 7 Abril 2011, 21:36 pm
.
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.

Código
  1.  
  2. Option Explicit
  3.  
  4. Public Enum tBase
  5.    [  Base2  ] = 2
  6.    [  Base8  ] = 8
  7.    [  Base10  ] = 10  '   //  Se obvia como entrada    (La dejo solo para verificar cogruencia)
  8.    [  Base16  ] = 16
  9. End Enum
  10.  
  11. Const conlnLong         As Long = &H20  '   //  Bites de un Long.
  12.  
  13. Public Function Dec2Bin(ByVal CurVal As Long) As String
  14. Dim lng_lim                         As Long
  15. Dim lng_index                       As Long
  16. Dim byt_res                         As Byte
  17. Dim boo_min                         As Boolean
  18.    If (CurVal < 0) Then
  19.        boo_min = True
  20.        CurVal = CurVal * -1 - 1
  21.        Dec2Bin = String$(conlnLong, "1")
  22.    Else
  23.        Dec2Bin = String$(conlnLong, "0")
  24.    End If
  25.    For lng_index = 0 To conlnLong - 1
  26.        byt_res = CurVal Mod 2
  27.        If (boo_min = True) Then
  28.            If (byt_res = 1) Then
  29.                byt_res = 0
  30.            Else
  31.                byt_res = 1
  32.            End If
  33.            Mid$(Dec2Bin, conlnLong - lng_index, 1) = byt_res
  34.        Else
  35.            Mid$(Dec2Bin, conlnLong - lng_index, 1) = byt_res
  36.        End If
  37.        CurVal = CurVal \ 2
  38.        If (CurVal = 0) Then Exit For
  39.    Next
  40. End Function
  41.  
  42. '   //  Hex no afecta a bases inferiores por ello lo dejo.
  43. Public Function Base2Dec(ByRef inval As String, ByRef InBase As tBase) As Long
  44. Dim lng_lenStr&
  45. Dim lng_Pointer&
  46. Dim lng_Potencia&
  47. Dim lng_limit&
  48.  
  49.    lng_lenStr& = Len(inval)
  50.  
  51.    If (lng_lenStr& >= conlnLong) Then
  52.        lng_lenStr& = conlnLong
  53.        lng_limit& = 2
  54.    Else
  55.        lng_limit& = InStr(1, inval, "-")
  56.    End If
  57.  
  58.    lng_Potencia& = 0
  59.  
  60.    For lng_Pointer& = lng_lenStr& To lng_limit& Step -1
  61.       Base2Dec = Base2Dec + CLng("&H" & Mid$(inval, lng_Pointer, 1)) * InBase ^ lng_Potencia&
  62.        lng_Potencia& = lng_Potencia& + 1
  63.    Next lng_Pointer&
  64.  
  65.    If (Mid$(inval, 1, 1) = "1") Then Base2Dec = Base2Dec * -1
  66.  
  67. End Function
  68.  
  69. Public Function AndAlt(Byte1 As Long, Byte2 As Long) As Long
  70. Dim str1        As String
  71. Dim str2        As String
  72. Dim str3        As String * conlnLong
  73. Dim i           As Byte
  74.  
  75.    str1 = Dec2Bin(Byte1)
  76.    str2 = Dec2Bin(Byte2)
  77.  
  78.    For i = 1 To conlnLong
  79.        If (Mid$(str1, i, 1) = "1") Then
  80.            If (Mid$(str1, i, 1) = Mid$(str2, i, 1)) Then
  81.                Mid$(str3, i, 1) = 1
  82.            Else
  83.                Mid$(str3, i, 1) = 0
  84.            End If
  85.        Else
  86.            Mid$(str3, i, 1) = 0
  87.        End If
  88.    Next i
  89.  
  90.    AndAlt = Base2Dec(str3, [  Base2  ])
  91.  
  92. End Function
  93.  
  94. Public Function OrAlt(Byte1 As Long, Byte2 As Long) As Long
  95. Dim str1        As String
  96. Dim str2        As String
  97. Dim str3        As String * conlnLong
  98. Dim i           As Byte
  99.  
  100.    str1 = Dec2Bin(Byte1)
  101.    str2 = Dec2Bin(Byte2)
  102.  
  103.    For i = 1 To conlnLong
  104.        If (Mid$(str1, i, 1) = "1") Then
  105.            Mid$(str3, i, 1) = 1
  106.        ElseIf (Mid$(str2, i, 1) = "1") Then
  107.            Mid$(str3, i, 1) = 1
  108.        Else
  109.            Mid$(str3, i, 1) = 0
  110.        End If
  111.    Next i
  112.  
  113.    OrAlt = Base2Dec(str3, [  Base2  ])
  114.  
  115. End Function
  116.  
  117. Public Function XorAlt(Byte1 As Long, Byte2 As Long) As Long
  118. Dim str1        As String
  119. Dim str2        As String
  120. Dim str3        As String * conlnLong
  121. Dim i           As Byte
  122.  
  123.    str1 = Dec2Bin(Byte1)
  124.    str2 = Dec2Bin(Byte2)
  125.  
  126.    For i = 1 To conlnLong
  127.        If (Mid$(str1, i, 1) = "1") Then
  128.            If (Mid$(str2, i, 1) = "0") Then
  129.                Mid$(str3, i, 1) = 1
  130.            Else
  131.                Mid$(str3, i, 1) = 0
  132.            End If
  133.        Else
  134.            If (Mid$(str2, i, 1) = "1") Then
  135.                Mid$(str3, i, 1) = 1
  136.            Else
  137.                Mid$(str3, i, 1) = 0
  138.            End If
  139.        End If
  140.    Next i
  141.  
  142.    XorAlt = Base2Dec(str3, [  Base2  ])
  143.  
  144. End Function
  145.  
  146. Public Function NotAlt(Byte1 As Long) As Long
  147.    NotAlt = (Byte1 + 1) * -1
  148. End Function
  149.  
  150.  

Ejemplo Basico:

Código
  1.  
  2. Private Sub Form_Load()
  3.    MsgBox XorAlt(AndAlt(NotAlt(7451), OrAlt(10, 456)), 1) & vbNewLine & _
  4.           CStr((Not 7451) And (10 Or 456) Xor 1)
  5. End Sub
  6.  
  7.  

claro esta que los operadores binarios son los mas rapidos y estos ni ninguno va asuperar la velocidad de los originales.

Temibles Lunas!ˇ.


Título: Re: [RETO] Reemplazo de Operadores Binarios.
Publicado por: 79137913 en 8 Abril 2011, 14:12 pm
HOLA!!!

Aca les dejo mi solucion:

Las funciones son independientes...

Al igual que Black 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.

Código
  1. Private Function AndAlt(Byte1 As Long, Byte2 As Long) As Long
  2. Dim bit1() As Boolean
  3. Dim bit2() As Boolean
  4. Dim bit3() As Boolean
  5. Dim CT     As Long
  6. Dim Tam    As Long
  7. Dim b1     As Long
  8. Dim b2     As Long
  9. b1 = Byte1
  10. b2 = Byte2
  11.    Do
  12.        ReDim Preserve bit1(CT)
  13.        If b1 = 1 Then ReDim Preserve bit1(CT): bit1(CT) = True: Exit Do
  14.        If b1 = 0 Then ReDim Preserve bit1(CT): Exit Do
  15.        bit1(CT) = CBool(b1 Mod 2)
  16.        b1 = Fix(b1 / 2)
  17.        CT = CT + 1
  18.    Loop
  19.    CT = 0
  20.    Do
  21.        If b2 = 1 Then ReDim Preserve bit2(CT): bit2(CT) = True: Exit Do
  22.        If b2 = 0 Then ReDim Preserve bit2(CT): Exit Do
  23.        ReDim Preserve bit2(CT)
  24.        bit2(CT) = CBool(b2 Mod 2)
  25.        b2 = Fix(b2 / 2)
  26.        CT = CT + 1
  27.    Loop
  28.    If UBound(bit1) > UBound(bit2) Then ReDim Preserve bit2(UBound(bit1))
  29.    If UBound(bit1) < UBound(bit2) Then ReDim Preserve bit1(UBound(bit2))
  30.    Tam = UBound(bit1)
  31.    ReDim bit3(Tam)
  32.    For X = 0 To Tam
  33.        If bit1(X) Then If bit2(X) Then bit3(X) = True
  34.    Next
  35.    For X = 0 To Tam
  36.        If bit3(X) Then AndAlt = AndAlt + 2 ^ (X)
  37.    Next
  38.  
  39. End Function
  40.  
  41. Private Function OrAlt(Byte1 As Long, Byte2 As Long) As Long
  42. Dim bit1() As Boolean
  43. Dim bit2() As Boolean
  44. Dim bit3() As Boolean
  45. Dim CT     As Long
  46. Dim Tam    As Long
  47. Dim b1     As Long
  48. Dim b2     As Long
  49. b1 = Byte1
  50. b2 = Byte2
  51.    Do
  52.        ReDim Preserve bit1(CT)
  53.        If b1 = 1 Then ReDim Preserve bit1(CT): bit1(CT) = True: Exit Do
  54.        If b1 = 0 Then ReDim Preserve bit1(CT): Exit Do
  55.        bit1(CT) = CBool(b1 Mod 2)
  56.        b1 = Fix(b1 / 2)
  57.        CT = CT + 1
  58.    Loop
  59.    CT = 0
  60.    Do
  61.        If b2 = 1 Then ReDim Preserve bit2(CT): bit2(CT) = True: Exit Do
  62.        If b2 = 0 Then ReDim Preserve bit2(CT): Exit Do
  63.        ReDim Preserve bit2(CT)
  64.        bit2(CT) = CBool(b2 Mod 2)
  65.        b2 = Fix(b2 / 2)
  66.        CT = CT + 1
  67.    Loop
  68.    If UBound(bit1) > UBound(bit2) Then ReDim Preserve bit2(UBound(bit1))
  69.    If UBound(bit1) < UBound(bit2) Then ReDim Preserve bit1(UBound(bit2))
  70.    Tam = UBound(bit1)
  71.    ReDim bit3(Tam)
  72.    For X = 0 To Tam
  73.        If bit1(X) Then bit3(X) = True
  74.        If bit2(X) Then bit3(X) = True
  75.    Next
  76.    For X = 0 To Tam
  77.        If bit3(X) Then OrAlt = OrAlt + 2 ^ (X)
  78.    Next
  79.  
  80. End Function
  81.  
  82. Private Function XorAlt(Byte1 As Long, Byte2 As Long) As Long
  83. Dim bit1() As Boolean
  84. Dim bit2() As Boolean
  85. Dim bit3() As Boolean
  86. Dim CT     As Long
  87. Dim Tam    As Long
  88. Dim b1     As Long
  89. Dim b2     As Long
  90. b1 = Byte1
  91. b2 = Byte2
  92.    Do
  93.        ReDim Preserve bit1(CT)
  94.        If b1 = 1 Then ReDim Preserve bit1(CT): bit1(CT) = True: Exit Do
  95.        If b1 = 0 Then ReDim Preserve bit1(CT): Exit Do
  96.        bit1(CT) = CBool(b1 Mod 2)
  97.        b1 = Fix(b1 / 2)
  98.        CT = CT + 1
  99.    Loop
  100.    CT = 0
  101.    Do
  102.        If b2 = 1 Then ReDim Preserve bit2(CT): bit2(CT) = True: Exit Do
  103.        If b2 = 0 Then ReDim Preserve bit2(CT): Exit Do
  104.        ReDim Preserve bit2(CT)
  105.        bit2(CT) = CBool(b2 Mod 2)
  106.        b2 = Fix(b2 / 2)
  107.        CT = CT + 1
  108.    Loop
  109.    If UBound(bit1) > UBound(bit2) Then ReDim Preserve bit2(UBound(bit1))
  110.    If UBound(bit1) < UBound(bit2) Then ReDim Preserve bit1(UBound(bit2))
  111.    Tam = UBound(bit1)
  112.    ReDim bit3(Tam)
  113.    For X = 0 To Tam
  114.        If bit1(X) Then If bit2(X) = False Then bit3(X) = True
  115.        If bit2(X) Then If bit1(X) = False Then bit3(X) = True
  116.    Next
  117.    For X = 0 To Tam
  118.        If bit3(X) Then XorAlt = XorAlt + 2 ^ (X)
  119.    Next
  120.  
  121. End Function
  122.  
  123. Private Function NotAlt(Byte1 As Long) As Long
  124.    NotAlt = -(Byte1 + 1)
  125. End Function
  126.  

GRACIAS POR LEER!!!