I am trying to do
and eax, 0fh
and I know 0fh
means 15
.
From
and eax, 0fh
I am supposed to clear all but the last 4 bits of eax
. I don't understand how you get (32-4)=28 bits of 1 and 4 0s.
I am trying to do
and eax, 0fh
and I know 0fh
means 15
.
From
and eax, 0fh
I am supposed to clear all but the last 4 bits of eax
. I don't understand how you get (32-4)=28 bits of 1 and 4 0s.
15 in binary is 1111
(in facts 15=8+4+2+1); adding the padding zeroes to reach 32 bits (the width of eax
) we get
00000000000000000000000000001111
Now, if you do a binary and
with the content of eax, obviously you clear all the bits of eax
but the last 4, since the zeroes "kill" whatever digit is in the corresponding position, while the ones let it "survive".
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx AND
00000000000000000000000000001111
----------------------------------------
0000000000000000000000000000xxxx
You don't get 28 1-bits followed by 4 0-bits, it's the other way around. 0fh
is:
000...0001111
When you and
something with a 1-bit, you get that something back. When you and
it with a 0-bit, you always get zero (see here for a treatise on the bitwise operators).
AND
ing a number with 000..001111
will therefore clear all but the rightmost 4 bits.
For brevity, lets look at this instruction instead:
and al, 0fh
0fh
translates to 00001111
in binary.
Now regardless of what x
is in each bit, this is what we get in binary:
al xxxxxxxx
0fh 00001111
------------
and 0000xxxx
Concrete example:
al 01010101
0fh 00001111
------------
and 00000101