Foro de elhacker.net

Programación => Programación Visual Basic => Mensaje iniciado por: BlackZeroX en 5 Junio 2011, 08:07 am



Título: Recopilacion de Funciones con operaciones Binarias.
Publicado por: BlackZeroX en 5 Junio 2011, 08:07 am
Bueno ya sabemos que las funciones con operaciones binarias son mas rápidas y mas practicas a la hora de ejecutarse.

La intención de este tema es que se creen una sola publicacion donde se pueden encontrar estas funciones de manera amena.

Código
  1.  
  2. '   //  Para valores tipo Long
  3. Private Sub lSwap(ByRef lVal1 As Long, ByRef lVal2 As Long)
  4.    '   //  Intercambia {lVal1} por {lVal2} y {lVal2} a {lVal1} sin variable temporal
  5.    lVal1 = lVal1 Xor lVal2
  6.    lVal2 = lVal2 Xor lVal1
  7.    lVal1 = lVal1 Xor lVal2
  8. End Sub
  9. Private Function lIsNegative(ByRef lVal As Long)
  10.    '   //  Para cualquier valor que lVal pueda tomar.
  11.    '   //  Comprueba si lval es negativo.
  12.    lIsNegative = (lVal And &H80000000)
  13. End Function
  14.  
  15. Private Function iIsNegative(ByRef iVal As Integer) As Boolean
  16.    '   //  Para cualquier valor que iVal pueda tomar.
  17.    '   //  Comprueba si lval es negativo.
  18.    iIsNegative = (iVal And 32768)
  19. End Function
  20.  
  21. Private Sub iSwap(ByRef iVal1 As Integer, ByRef iVal2 As Integer)
  22.    '   //  Intercambia {iVal1} por {iVal2} y {iVal2} a {iVal1} sin variable temporal
  23.    iVal1 = iVal1 Xor iVal2
  24.    iVal2 = iVal2 Xor iVal1
  25.    iVal1 = iVal1 Xor iVal2
  26. End Sub
  27.  
  28. Private Sub bSwap(ByRef iVal1 As byte, ByRef iVal2 As byte)
  29.    '   //  Intercambia {iVal1} por {iVal2} y {iVal2} a {iVal1} sin variable temporal
  30.    iVal1 = iVal1 Xor iVal2
  31.    iVal2 = iVal2 Xor iVal1
  32.    iVal1 = iVal1 Xor iVal2
  33. End Sub
  34.  
  35. Function max(ByVal val1 As Long, ByVal val2 As Long) As Long
  36.    If (val1 > val2) Then
  37.        max = val1
  38.    Else
  39.        max = val2
  40.    End If
  41. End Function
  42.  
  43. Function min(ByVal val1 As Long, ByVal val2 As Long) As Long
  44.    If (val1 > val2) Then
  45.        min = val2
  46.    Else
  47.        min = val1
  48.    End If
  49. End Function
  50.  
  51. Function bSwapBit(ByVal myLong As Long, ByVal bit1 As Byte, ByVal bit2 As Byte) As Long
  52. '   Los bits se CUENTAS DE DERECHA A IZQUIERDA es decir:    31, 30, ... , 3, 2, 1, 0
  53. '   Solo se admite rango 0 al 31.
  54. Dim aux As Long
  55. Dim mask As Long
  56.  
  57.    aux = max(bit1, bit2)
  58.    bit2 = min(bit1, bit2)
  59.    bit1 = aux  '   max
  60.    Debug.Assert (bit1 > 31)    '   No se permiten numero mayores a 32
  61.    Debug.Assert (bit2 < 0)     '   No se permiten valores negativos
  62.    mask = Not ((2 ^ bit1) Or (2 ^ bit2))
  63.    aux = (2 ^ (bit1 - bit2))
  64.    bSwapBit = (myLong And mask) Or _
  65.               (myLong And (2 ^ bit1)) / aux Or _
  66.               (myLong And (2 ^ bit2)) * aux
  67. End Function
  68.  
  69.  

Si alguien se sabe mas y quiere aportarlas están en el lugar indicado.

Temibles Lunas!¡.


Título: Re: Recopilacion de Funciones con operaciones Binarias.
Publicado por: BlackZeroX en 8 Junio 2011, 04:18 am
Código
  1.  
  2. Public Function LongToByte(ByVal lVal As Long) As Byte()
  3. Dim bRet(0 To 3)        As Byte
  4.    bRet(3) = (lVal And &HFF000000) \ &H1000000
  5.    bRet(2) = (lVal And &HFF0000) \ &H10000
  6.    bRet(1) = (lVal And &HFF00&) \ &H100
  7.    bRet(0) = (lVal And &HFF)
  8.    LongToByte = bRet
  9. End Function
  10.  
  11.  

Código
  1.  
  2. Private sub ColorLongToRGB(ByVal LngColor As Long, ByRef OutRed As Byte, ByRef OutGreen As Byte, ByRef OutBlue As Byte)
  3.   OutBlue = (LngColor And &HFF0000) \ &H10000
  4.   OutGreen = (LngColor And &HFF00&) \ &H100
  5.   OutRed = (LngColor And &HFF)
  6. End sub
  7.  
  8.  

Dulces Lunas!¡.


Título: Re: Recopilacion de Funciones con operaciones Binarias.
Publicado por: BlackZeroX en 10 Junio 2011, 04:14 am

Cambio rapido del signo a un valor dado N ( habitualmente:  lval=(lval*(-1)) )

Código
  1.  
  2. Private Sub lChangeSign(ByRef lVal As Long)
  3.    '   //  Para cualquier valor que lVal pueda tomar.
  4.    '   //  Cambia de signo a un numero( + a - y de - a +).
  5.    lVal = ((Not lVal) + 1)
  6. End Sub
  7. '   //  Para valores tipo Integer
  8. Private Sub iChangeSign(ByRef iVal As Integer)
  9.    '   //  Para cualquier valor que iVal pueda tomar.
  10.    '   //  Cambia de signo a un numero( + a - y de - a +).
  11.    lVal = ((Not lVal) + 1)
  12. End Sub
  13.  
  14.  

Dulce sLunas!¡.


Título: Re: Recopilacion de Funciones con operaciones Binarias.
Publicado por: raul338 en 10 Junio 2011, 04:55 am
Le puse chincheta :P

No seria lo mismo (despreciando la velocidad) si en lugar de tener 2 firmas, una para long y otra para integer. Usar & en su lugar? (Mr Frog habria usado eso alguna vez)

Ej
Sub xxx(ByRef val1&, ByRef val2&)


Título: Re: Recopilacion de Funciones con operaciones Binarias.
Publicado por: BlackZeroX en 10 Junio 2011, 06:57 am
& = as long, es lo mismo...

http://wiki.elhacker.net/programacion/vb/4---principios-basicos

Spyke1 - (Alias Mr. Frogs) me copio eso; pero ya entendí que mejor declaro bien y uso la técnica de declaración Hugara (o alguna nomenclatura simple pero concreta) en lugar de los signos al final de una variable, con excepciones por ejemplo en las funciones LongToByte y ColorLongToRGB la Mascara que se efectúa con &HFF00& para obtener los Bits deseados, tendría que ser una mascara tipo Long por ello se le pone el signo & ya que si no se le pone pasa a tratarse como un valor integer, solo para casos como estos se usa dicho signo.

Código
  1.  
  2. msgbox typename(&HFF00&)
  3. msgbox typename(&HFF00)
  4.  
  5.  

Dulces Lunas!¡.


Título: Re: Recopilacion de Funciones con operaciones Binarias.
Publicado por: BlackZeroX en 10 Junio 2011, 20:29 pm
.
Alternativa a htons@Ws2_32 (API) (http://msdn.microsoft.com/en-us/library/ms738557(VS.85).aspx)

http://foro.elhacker.net/programacion_visual_basic/vbsnippet_htons_replacement-t297824.0.html
PAra quienes no lo entiendan o lo vean demasiado Revuelto el codigo original esta en esta web:
http://www.xbeat.net/vbspeed/c_SwapEndian.htm

' by Mike D Sutton, Mike.Sutton@btclick.com, 20040914

Código
  1.  
  2. Public Function SwapEndian08(ByVal dw As Long) As Long
  3. ' by Mike D Sutton, Mike.Sutton@btclick.com, 20040914
  4.  SwapEndian08 = _
  5.      (((dw And &HFF000000) \ &H1000000) And &HFF&) Or _
  6.      ((dw And &HFF0000) \ &H100&) Or _
  7.      ((dw And &HFF00&) * &H100&) Or _
  8.      ((dw And &H7F&) * &H1000000)
  9.  If (dw And &H80&) Then SwapEndian08 = SwapEndian08 Or &H80000000
  10. End Function
  11.  
  12.  

Código
  1.  
  2. Public Function htons(ByVal lPort As Long) As Integer
  3.    htons = ((((lPort And &HFF000000) \ &H1000000) And &HFF&) Or ((lPort And &HFF0000) \ &H100&) Or ((lPort And &HFF00&) * &H100&) Or ((lPort And &H7F&) * &H1000000) Or (IIf((lPort And &H80&), &H80000000, &H0)) And &HFFFF0000) \ &H10000
  4. End Function
  5.  
  6.  

Dulces Lunas!¡.


Título: Re: Recopilacion de Funciones con operaciones Binarias.
Publicado por: BlackZeroX en 13 Agosto 2011, 07:21 am
macro de C/C++ muy usada con el API SendMessage().

Código
  1.  
  2. Function makelParam(ByVal L As Integer, ByVal U As Integer) As Long
  3.   makelParam = L Or (U * &H10000)
  4. End Function
  5.  
  6.  

Dulces Lunas!¡.


Título: Re: Recopilacion de Funciones con operaciones Binarias.
Publicado por: BlackZeroX en 14 Agosto 2011, 02:52 am
.
Recreacion de la funcion isNumeric(), empleando operaciones a nivel Bit
IsNumeric() (http://foro.elhacker.net/programacion_visual_basic/reto_reemplazo_de_funcion_isnumeric-t336067.0.html;msg1651317#msg1651317)

Variable lData..

Por que no usar Dim byData(3) as byte y quitar las mascaras de bytes?
R: Es mas lento, ¿por que?, me parece que es por que se involucra una multiplicacion aparentemente, o eso quiero creer, aun asi ya lo probe y si es mas leeeento.

Por que no usar otras 2 variables para que sea mas legible?
R: Es un ejemplo de como usar una variable tipo long para que la misma tenga distintos usos, no solo uno, ademas las mascaras son tan rapidas que no influyen en la velocidad.

Extructura de la variable lData

Para la explicacion veremos la variable de manera binaria:
0000 0000 0000 0000 0000 0000 0000 0000

0000 0000 => sección de 1 Byte donde se guarda el caracterleido con el API RtlMoveMemory().
0000 0000 => sección Flags de 1 Byte, se usa para guardar los Flags siguientes:
Código
  1.  
  2. Const PUNTO_DECIMAL As Long = &H10000
  3. Const SIGNO_SRC     As Long = &H20000
  4. Const NUMBER_HEX    As Long = &H40000
  5. Const NUMBER_OK     As Long = &H80000
  6. Const NUMBER_POW    As Long = &H100000
  7. Const NUMBER_POWF   As Long = &H200000
  8. Const NUMBER_POWC   As Long = &H300000
  9. Const NUMBER_FINISH As Long = &H400000
  10.  
  11.  

0000 0000 => sección 1 Byte (No tiene uso pero puede servir para continuar el conteo de la siguiente sección 0000 0000).

0000 0000 => sección 1 Byte, Se usa como contador sin signo con limite 2 potencia 8 es decir de 0 a 255 ( gracias a que el siguiente bloque 0000 0000 no se usa se puede expandir a 2 potencia 16 es decir 0 a 65535), se púso el contador en esta sección ya que la suma seria directa sin mascara alguna o algun tipo de dezplazamiento de bits y de esta manera NO MODIFICARIA los siguientes bloques de bytes.

Código
  1.  
  2. lData = (lData + &H1)
  3.  
  4.  

Temibles Lunas!¡.


Título: Re: Recopilacion de Funciones con operaciones Binarias.
Publicado por: BlackZeroX en 28 Septiembre 2011, 08:48 am
.
Sumar Dos colores... No trabaja aun muy bien que digamos... si desean discutir este algoritmo creen un nuevo tema, gracias!¡.

Código
  1.  
  2. Option Explicit
  3.  
  4. Private Function SumarColor(ByVal lColor As Long, ByVal AddColor As Long) As Long
  5. Dim lRetColor           As Long
  6.    If (lColor) Then
  7.        If ((lColor And &HFF&) = &H0) Then
  8.            lRetColor = (AddColor And &HFF&)
  9.        ElseIf ((AddColor And &HFF&) = &H0) Then
  10.            lRetColor = (lColor And &HFF&)
  11.        Else
  12.            lRetColor = (((lColor And &HFF&) + (AddColor And &HFF&)) \ 2)
  13.        End If
  14.  
  15.        If ((lColor And &HFF00&) = &H0) Then
  16.            lRetColor = (lRetColor Or (AddColor And &HFF00&))
  17.        ElseIf ((AddColor And &HFF00&) = &H0) Then
  18.            lRetColor = (lRetColor Or (lColor And &HFF00&))
  19.        Else
  20.            lRetColor = (lRetColor Or (((((lColor And &HFF00&) \ &H100&) + ((AddColor And &HFF00&) \ &H100&)) \ 2) * &H100&))
  21.        End If
  22.  
  23.        If ((lColor And &HFF0000) = &H0) Then
  24.            lRetColor = (lRetColor Or (AddColor And &HFF0000))
  25.        ElseIf ((AddColor And &HFF0000) = &H0) Then
  26.            lRetColor = (lRetColor Or (lColor And &HFF0000))
  27.        Else
  28.            lRetColor = (lRetColor Or (((((lColor And &HFF0000) \ &H10000) + ((AddColor And &HFF0000) \ &H10000)) \ 2) * &H10000))
  29.        End If
  30.  
  31.        If ((lColor And &HFF000000) = &H0) Then
  32.            lRetColor = (lRetColor Or (AddColor And &HFF000000))
  33.        ElseIf ((AddColor And &HFF000000) = &H0) Then
  34.            lRetColor = (lRetColor Or (lColor And &HFF000000))
  35.        Else
  36.            lRetColor = (lRetColor Or (((((lColor And &HFF000000) \ &H1000000) + ((AddColor And &HFF000000) \ &H1000000)) \ 2) * &H1000000))
  37.        End If
  38.    Else
  39.        lRetColor = AddColor
  40.    End If
  41.    SumarColor = lRetColor
  42. End Function
  43.  
  44.  

Código
  1.  
  2. Private Sub Form_Load()
  3.    Show
  4.    BackColor = SumarColor(RGB(255, 0, 0), RGB(0, 255, 0))
  5.    BackColor = SumarColor(BackColor, RGB(0, 0, 255))
  6.    BackColor = SumarColor(BackColor, RGB(0, 25, 0))
  7.    BackColor = SumarColor(BackColor, RGB(0, 25, 10))
  8.    BackColor = SumarColor(BackColor, RGB(0, 1, 4))
  9.    BackColor = SumarColor(BackColor, RGB(30, 0, 0))
  10. End Sub
  11.  
  12.  

Temibles Lunas!¡.


Título: Re: Recopilacion de Funciones con operaciones Binarias.
Publicado por: BlackZeroX en 21 Octubre 2011, 07:03 am
* Rotar Bits en distintas longitudes 8, 16, 32 y 64 bits (http://foro.elhacker.net/programacion_visual_basic/sources_code_rotbits_byte_to_byte-t342467.0.html;msg1676295#msg1676295)

Dulces Lunas!¡.


Título: Re: Recopilacion de Funciones con operaciones Binarias.
Publicado por: arfgh en 20 Febrero 2012, 14:21 pm
Este tópico es genial, no obstante estaría bien que pusieseis también las operaciones con bits tipo shr y shl.


Título: Re: Recopilacion de Funciones con operaciones Binarias.
Publicado por: BlackZeroX en 4 Junio 2012, 09:34 am
Alternativa a la función Xor...

Código
  1.  
  2. Option Explicit
  3.  
  4. Private Sub Form_Load()
  5. Const a As Long = 0
  6. Const b As Long = 1
  7.    MsgBox Xor_alt(a, b) & vbCrLf & (a Xor b)
  8. End Sub
  9.  
  10. Public Function Xor_alt(ByVal n1 As Long, ByVal n2 As Long) As Long
  11.    Xor_alt = (Not n1) And n2 Or (Not n2) And n1
  12. End Function
  13.  
  14.  
  15.  

P.D.: Necesito crearle un Indice a este tema... cuando tenga tiempo libre lo haré...

Dulces Lunas!¡.


Título: Re: Recopilacion de Funciones con operaciones Binarias.
Publicado por: 79137913 en 4 Junio 2012, 15:57 pm
HOLA!!!

Deberias agregar el reto de reemplazo de operadores binarios:


And, Not, Xor y Or reemplazados:
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

GRACIAS POR LEER!!!


Título: Re: Recopilacion de Funciones con operaciones Binarias.
Publicado por: Psyke1 en 19 Enero 2013, 01:23 am
Un oneliner que he tenido que crear para un proyecto en curso.
Devuelve la diferencia entre dos colores con un número del 0 al 100 según el porcentaje.

Código
  1. Public Function ColorDiff(ByVal lC1 As Long, ByVal lC2 As Long) As Single
  2.    ColorDiff = &H64 - &H64 * (Abs((lC1 And &HFF) - (lC2 And &HFF)) + Abs(((lC1 \ &H100) And &HFF) - ((lC2 \ &H100) And &HFF)) + Abs(((lC1 \ &H10000) And &HFF) - ((lC2 \ &H10000) And &HFF))) / &H2FD
  3. End Function

DoEvents! :P


Título: Re: Recopilacion de Funciones con operaciones Binarias.
Publicado por: pkj en 17 Mayo 2015, 12:14 pm
Es una buena idea, pero podriais corregir los fallos gordos, que aqui dejan editar :P

Una sub? mas bien no
Código:
Private Sub lIsNegative(ByRef lVal As Long)

    '   //  Para cualquier valor que lVal pueda tomar.

    '   //  Comprueba si lval es negativo.

    lIsNegative = (lVal And &H80000000)

End Sub

Una Sub con End Function?
Código:
Private sub ColorLongToRGB(ByVal LngColor As Long, ByRef OutRed As Byte, ByRef OutGreen As Byte, ByRef OutBlue As Byte)

   OutBlue = (LngColor And &HFF0000) \ &H10000

   OutGreen = (LngColor And &HFF00&) \ &H100

   OutRed = (LngColor And &HFF)

End Function

Saludos

EDIT:

Para que veais que no solo me gusta criticar, aprovecho para dejaros mi version super cutre de los operadores And, Or, Xor y Not.
Es muy rustica pero no contiene ni un And, Or, Xor, Not y parece funcionar con positivos, negativos y mezclas y ya de paso incluye las conversiones Bin2Hex, Hex2Bin, etc...

Código
  1. Private Function OrAlt(ByVal Valor1 As Long, ByVal Valor2 As Long) As Long
  2.  Dim V1 As String
  3.  Dim V2 As String
  4.  V1 = Dec2Bin(Valor1)
  5.  V2 = Dec2Bin(Valor2)
  6.  
  7.  Dim UnBit As String
  8.  Dim Res As String
  9.  Dim F As Integer
  10.  For F = 1 To Len(V1)
  11.    UnBit = "0"
  12.    If Mid(V1, F, 1) = 1 Then UnBit = "1"
  13.    If Mid(V2, F, 1) = 1 Then UnBit = "1"
  14.    Res = Res & UnBit
  15.  Next F
  16.  
  17.  OrAlt = Bin2Dec(Res)
  18.  
  19. End Function
  20.  
  21. Private Function AndAlt(ByVal Valor1 As Long, ByVal Valor2 As Long) As Long
  22.  Dim V1 As String
  23.  Dim V2 As String
  24.  V1 = Dec2Bin(Valor1)
  25.  V2 = Dec2Bin(Valor2)
  26.  
  27.  Dim UnBit As String
  28.  Dim CuentaOK As Integer
  29.  Dim Res As String
  30.  Dim F As Integer
  31.  For F = 1 To Len(V1)
  32.    CuentaOK = 0
  33.    UnBit = "0"
  34.    If Mid(V1, F, 1) = 1 Then CuentaOK = CuentaOK + 1
  35.    If Mid(V2, F, 1) = 1 Then CuentaOK = CuentaOK + 1
  36.    If CuentaOK = 2 Then UnBit = "1"
  37.    Res = Res & UnBit
  38.  Next F
  39.  
  40.  AndAlt = Bin2Dec(Res)
  41.  
  42. End Function
  43.  
  44. Private Function XorAlt(ByVal Valor1 As Long, ByVal Valor2 As Long) As Long
  45.  Dim V1 As String
  46.  Dim V2 As String
  47.  V1 = Dec2Bin(Valor1)
  48.  V2 = Dec2Bin(Valor2)
  49.  
  50.  Dim UnBit As String
  51.  Dim CuentaOK As Integer
  52.  Dim Res As String
  53.  Dim F As Integer
  54.  For F = 1 To Len(V1)
  55.    CuentaOK = 0
  56.    UnBit = "0"
  57.    If Mid(V1, F, 1) = 1 Then CuentaOK = CuentaOK + 1
  58.    If Mid(V2, F, 1) = 1 Then CuentaOK = CuentaOK + 1
  59.    If CuentaOK = 1 Then UnBit = "1"
  60.    Res = Res & UnBit
  61.  Next F
  62.  
  63.  XorAlt = Bin2Dec(Res)
  64.  
  65. End Function
  66.  
  67. Private Function NotAlt(ByVal Valor1 As Long) As Long
  68.  Dim V1 As String
  69.  Dim V2 As String
  70.  V1 = Dec2Bin(Valor1)
  71.  
  72.  Dim UnBit As String
  73.  Dim Res As String
  74.  Dim F As Integer
  75.  For F = 1 To Len(V1)
  76.    If Mid(V1, F, 1) = "1" Then
  77.      UnBit = "0"
  78.    Else
  79.      UnBit = "1"
  80.    End If
  81.    Res = Res & UnBit
  82.  Next F
  83.  
  84.  NotAlt = Bin2Dec(Res)
  85.  
  86. End Function
  87.  
  88. Function Bin2Dec(ByVal sBinario As String) As Long
  89.  'Bin2Dec = CDec("&H" & Bin2Hex(sBinario)) 'no hace falta el cdec :O
  90.  Bin2Dec = "&H" & Bin2Hex(sBinario)
  91. End Function
  92.  
  93. Public Function Dec2Bin(ByVal Valor As Long, Optional MinBits As Integer = 32) As String
  94.  Dec2Bin = Hex2Bin(Hex$(Valor))
  95.  Do Until Len(Dec2Bin) >= MinBits
  96.    Dec2Bin = "0" & Dec2Bin
  97.  Loop
  98. End Function
  99.  
  100. Function Bin2Hex(ByVal StrBin As String) As String
  101.  Dim F As Long
  102.  
  103.  Do Until Len(StrBin) / 4 = Len(StrBin) \ 4
  104.    StrBin = "0" & StrBin
  105.  Loop
  106.  For F = Len(StrBin) - 3 To 1 Step -4
  107.  
  108.    Select Case Mid$(StrBin, F, 4)
  109.      Case "0000"
  110.        Bin2Hex = "0" & Bin2Hex
  111.      Case "0001"
  112.        Bin2Hex = "1" & Bin2Hex
  113.      Case "0010"
  114.        Bin2Hex = "2" & Bin2Hex
  115.      Case "0011"
  116.        Bin2Hex = "3" & Bin2Hex
  117.      Case "0100"
  118.        Bin2Hex = "4" & Bin2Hex
  119.      Case "0101"
  120.        Bin2Hex = "5" & Bin2Hex
  121.      Case "0110"
  122.        Bin2Hex = "6" & Bin2Hex
  123.      Case "0111"
  124.        Bin2Hex = "7" & Bin2Hex
  125.      Case "1000"
  126.        Bin2Hex = "8" & Bin2Hex
  127.      Case "1001"
  128.        Bin2Hex = "9" & Bin2Hex
  129.      Case "1010"
  130.        Bin2Hex = "A" & Bin2Hex
  131.      Case "1011"
  132.        Bin2Hex = "B" & Bin2Hex
  133.      Case "1100"
  134.        Bin2Hex = "C" & Bin2Hex
  135.      Case "1101"
  136.        Bin2Hex = "D" & Bin2Hex
  137.      Case "1110"
  138.        Bin2Hex = "E" & Bin2Hex
  139.      Case "1111"
  140.        Bin2Hex = "F" & Bin2Hex
  141.  
  142.    End Select
  143.  Next F
  144.  
  145. End Function
  146.  
  147. Function Hex2Bin(ByVal CadenaHexadecimal As String) As String
  148.  Dim F As Long
  149.  
  150.  CadenaHexadecimal = UCase(CadenaHexadecimal)
  151.  
  152.  If Len(CadenaHexadecimal) > 0 Then
  153.    For F = Len(CadenaHexadecimal) To 1 Step -1
  154.      Select Case Mid$(CadenaHexadecimal, F, 1)
  155.        Case "0":
  156.          Hex2Bin = "0000" & Hex2Bin
  157.        Case "1":
  158.          Hex2Bin = "0001" & Hex2Bin
  159.        Case "2":
  160.          Hex2Bin = "0010" & Hex2Bin
  161.        Case "3":
  162.          Hex2Bin = "0011" & Hex2Bin
  163.        Case "4":
  164.          Hex2Bin = "0100" & Hex2Bin
  165.        Case "5":
  166.          Hex2Bin = "0101" & Hex2Bin
  167.        Case "6":
  168.          Hex2Bin = "0110" & Hex2Bin
  169.        Case "7":
  170.          Hex2Bin = "0111" & Hex2Bin
  171.        Case "8":
  172.          Hex2Bin = "1000" & Hex2Bin
  173.        Case "9":
  174.          Hex2Bin = "1001" & Hex2Bin
  175.        Case "A":
  176.          Hex2Bin = "1010" & Hex2Bin
  177.        Case "B":
  178.          Hex2Bin = "1011" & Hex2Bin
  179.        Case "C":
  180.          Hex2Bin = "1100" & Hex2Bin
  181.        Case "D":
  182.          Hex2Bin = "1101" & Hex2Bin
  183.        Case "E":
  184.          Hex2Bin = "1110" & Hex2Bin
  185.        Case "F":
  186.          Hex2Bin = "1111" & Hex2Bin
  187.      End Select
  188.  
  189.    Next F
  190.  End If
  191.  On Local Error GoTo 0
  192. End Function
  193.  

Saludos.