Você precisa de operadores Bitwise Shift no Excel VBA que é semelhante aos operadores << e >> em C ++, mas infelizmente, o VBA não possui nenhum operador de mudança de bit. Neste caso, podemos replicar operadores de deslocamento bit a bit, com multiplicação ou divisão pela potência apropriada de 2.
Função Bitwise Right Shift:
Public Function shr(ByVal Value As Long, ByVal Shift As Byte) As Long Dim i As Byte shr = Value If Shift > 0 Then shr = Int(shr / (2 ^ Shift)) End If End Function
A mudança à direita é igual a dividir o valor por 2Shift.
Função de deslocamento à esquerda bit a bit:
Public Function shl(ByVal Value As Long, ByVal Shift As Byte) As Long shl = Value If Shift > 0 Then Dim i As Byte Dim m As Long For i = 1 To Shift m = shl And &H40000000 shl = (shl And &H3FFFFFFF) * 2 If m <> 0 Then shl = shl Or &H80000000 End If Next i End If End Function
Deslocamento à esquerda é igual a multiplicar Valor por 2Shift. Mas, para evitar um erro de estouro, usamos um pequeno truque:
m = shl And &H40000000 ' save 30th bit shl = shl And &H3FFFFFFF ' clear 30th and 31st bits shl = shl * 2 ' multiply by 2 If m <> 0 Then shl = shl Or &H80000000 ' set 31st bit End If