Question

I'm trying to AND two hex number bit-wise in Excel/Visual Basic.

Ex. 53FDBC AND 00FFFF which should yield 00FDBC.

Any ideas on how to do this?

Was it helpful?

Solution

I assume you have your 2 values stored as strings. In this case you could do the following:

Dim hex1 As String
hex1 = "53FDBC"

Dim hex2 As String
hex2 = "00FFFF"

Dim bin1 As String
bin1 = CLng("&H" & hex1)

Dim bin2 As String
bin2 = CLng("&H" & hex2)

Dim result As String
result = Hex$(bin1 And bin2)

result now contains "FDBC", but you may wish to pad this to the left with zeroes

As a function (excel module) this could be implemented as:

Function hexand(hex1 As String, hex2 As String) as String

Dim bin1 As String
bin1 = CLng("&H" & hex1)

Dim bin2 As String
bin2 = CLng("&H" & hex2)

hexand = Hex$(bin1 And bin2)

End Function

OTHER TIPS

iirc, it's built-in, only bit shifting operators are not built-in

&H53FDBC And &H00FFFF

hex value of 0xFFFF in Decimal is 65536 hex value of 0x53FDBC in Decimal is 5504444

So you can use =DEC2HEX(MOD(5504444,65536))

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top