Click to See Complete Forum and Search --> : Hexadecimal Masking Question...


goatslayer
January 30th, 2008, 11:45 AM
I am using hex in a project I am working on at the moment. I came across this the other day and can't figure out, how the result is returned:

Why does:

0xbf201138 & 0x00FFFFFF

Equal:

0x201138

I think this is an example of masking, however I can't find any documentation on the net how it works.

If anyone can explain how 0x00FFFFFF removes the first byte of the hex value, leaving the last 3, it will be a great help.

Other than that, if you have any resources you can point me to, that will be great too.

Sorry if this seems a dumb question, but I haven't covered much hex before.

TheCPUWizard
January 30th, 2008, 11:53 AM
When you "and" (that is what & does) two values together, you only get a 1 if the bit was set in BOTH of the values (it was set in value1 AND value2).

Since the bits for the upper byte are not set in the mask (0x00ffffff) it is impossible for them to be set in the result, regardless of their state in the other value.

GremlinSA
January 30th, 2008, 02:23 PM
To expand on what CPUWiz posted here is an example in Binary (as hex is derived from 4 binary bits)......


110101100101 (0xD65)
&
000000111111 (0x(0)3f)
=
000000100101 (0x(0)25)

Hope this helps..

Gremmy...

goatslayer
January 31st, 2008, 07:24 PM
Thanks CPUWizard, and GremlinSA.

It might sound odd but I confused myself by not thinking in binary. All I could see were hex digits, and couldn't figure out what was going on.

Once you showed me that the mask ultimately maps to binary, then it makes sense.

(further I must add, google helped to confuse me by only showing 6 bits in the separate numbers when conveting to binary 0x20, 0x11 etc. (I should have written them down myself)).

Anyhow, this is a useful technique I will endeavour to use further in the future.

For completion, for any future readers of this thread, the solution is:

0xbf201138 & 0x00ffffff =

10111111 00100000 00010001 00111000
11111111 11111111 11111111
------------------------------------------------------
00100000 00010001 00111000
------------------------------------------------------
20 11 38

EQUALS: 0x201138

Sucessfully masking out 0xbf