1

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.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
user2149780
  • 175
  • 2
  • 3
  • 13

3 Answers3

5

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
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
1

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).

ANDing a number with 000..001111 will therefore clear all but the rightmost 4 bits.

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
1

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
Kendall Frey
  • 43,130
  • 20
  • 110
  • 148