brief Create a contiguous bitmask starting at bit position p l and ending at position p h #define GENMASKh l ~0UL - 1UL l + 1 & ~0UL BITS_PER_LONG - 1 - hh=7l=0
The code will generate a bitmask that has all bits set to 1 from bit position 0 to bit position 7 (inclusive).
The macro GENMASK(h, l) takes two arguments, h and l, which represent the highest and lowest bit positions respectively. The bitmask is created using bitwise operators.
First, the expression (1UL << (l)) shifts the number 1 to the left by l positions, creating a binary number with a single 1 bit at position l.
Next, this number is subtracted from ~0UL, which is a binary number with all bits set to 1. This creates a binary number with all 1 bits from bit position 0 to bit position l-1, and all 0 bits from bit position l to the end.
To extend the range of the bitmask, the expression (~0UL >> (BITS_PER_LONG - 1 - (h))) is used. This creates a binary number with all 1 bits from bit position h to the end, and all 0 bits from bit position 0 to bit position h-1.
Finally, the two binary numbers are combined using the bitwise AND operator &. The result is a binary number with all 1 bits from bit position l to bit position h, and all 0 bits elsewhere
原文地址: https://www.cveoy.top/t/topic/hdHf 著作权归作者所有。请勿转载和采集!