descriptor = limit & 0x000F0000;
descriptor |= (flag << 8) & 0x00F0FF00;
descriptor |= (base >> 16) & 0x000000FF;
descriptor |= base & 0xFF000000;
I understood the fact that the and operation is used for masking certain bits. But what is OR operation used here for??? Please elaborate.
This is part of the code for creating a Global Descriptor Table.
What the code you've shown is doing is constructing descriptor
by selecting different parts of it from other boolean expressions.
Notice that the constants that (flag << 8)
, (base >> 16)
and base
are being ANDed with, when themselves ORed together, produce 0xFFFFFFFF
.
The point of the OR is to say, "the first 8 bits come from (base >> 16)
, the next 8 bits from flag << 8
, the next 4 from limit
, the next 4 from flag << 8
and the last 8 from base
." So finally, descriptor looks like this:
d[7], d[6], b[5], a[4], b[3], b[2], c[1], c[0]
Where each comma separated variable is a hexadecimal digit, and a
, b
, c
, and d
are
limit
, (flag << 8)
, (base >> 16)
and base
respectively. (The commas are just there for readability, they stand for concatenation of the digits).