Bit Operation Library
XWOS Bit Operation Library
Categories:
3 minute read
Bit Operations
XWOS provides a bit operation template library. Various non-function-pointer types defined in Basic Types can use bit operations. The bit operation function templates include:
xwbop_s1m(): Set all bits in the data mask portion to 1xwbop_c0m(): Clear all bits in the data mask portion to 0xwbop_x1m(): Toggle all bits in the data mask portionxwbop_rbit(): Mirror-reverse the bits of the dataxwbop_re(): Reverse the endianness of the dataxwbop_ffs(): Find the first bit set to 1 starting from the least significant bitxwbop_fls(): Find the first bit set to 1 starting from the most significant bitxwbop_ffz(): Find the first bit cleared to 0 starting from the least significant bitxwbop_flz(): Find the first bit cleared to 0 starting from the most significant bitxwbop_weight(): Count the number of 1 bits in the data
The first parameter of all bit operation functions is the Basic Type, and the second parameter is the data or pointer to the data, for example:
xwssq_t idx;
/* Find the first bit set to 1 from the LSB in data "0" of type "xwu32_t", result is -1 */
idx = xwbop_ffs(xwu32_t, 0);
/* Find the first bit set to 1 from the LSB in data "0xF0" of type "xwu32_t", result is 4 */
idx = xwbop_ffs(xwu32_t, 0xF0);
/* Mirror-reverse data "0xAA" of type "xwu32_t", result is 0x55 */
xwu32_t rdata = xwbop_rbit(xwu32_t, 0xAA);
Bitmap Operations
XWOS provides the basic type xwbmp_t for defining bitmaps. A bitmap is an array of xwbmp_t.
Users can declare bitmaps of any number of bits. XWOS provides a function set for convenient bitmap operations:
xwbmpop_declare(): Declare a bitmapxwbmpop_assign(): Assignxwbmpop_cmp(): Compare two bitmapsxwbmpop_s1all(): Set all bits to 1xwbmpop_c0all(): Clear all bits to 0xwbmpop_s1i(): Set a certain bit in the bitmap to 1xwbmpop_s1m(): Set the mask portion of the bitmap to 1xwbmpop_c0i(): Clear a certain bit in the bitmap to 0xwbmpop_c0m(): Clear the mask portion of the bitmap to 0xwbmpop_x1i(): Toggle a certain bit in the bitmapxwbmpop_x1m(): Toggle the mask portion of the bitmapxwbmpop_t1i(): Test whether a certain bit in the bitmap is 1xwbmpop_t1ma(): Test whether all bits in the mask portion are 1xwbmpop_t1ma_then_c0m(): Test whether all bits in the mask portion are 1, and if so, clear the mask portion to 0xwbmpop_t1mo(): Test whether at least one bit in the mask portion is 1xwbmpop_t1mo_then_c0m(): Test whether at least one bit in the mask portion is 1, and if so, clear the mask portion to 0xwbmpop_t0ma(): Test whether all bits in the mask portion are 0xwbmpop_t0ma_then_s1m(): Test whether all bits in the mask portion are 0, and if so, set the mask portion to 1xwbmpop_t0mo(): Test whether at least one bit in the mask portion is 0xwbmpop_t0mo_then_s1m(): Test whether at least one bit in the mask portion is 0, and if so, set the mask portion to 1xwbmpop_not(): NOT operationxwbmpop_and(): AND operationxwbmpop_or(): OR operationxwbmpop_xor(): XOR operationxwbmpop_ffs(): Find the first bit set to 1 starting from the LSBxwbmpop_fls(): Find the first bit set to 1 starting from the MSBxwbmpop_ffz(): Find the first bit cleared to 0 starting from the LSBxwbmpop_flz(): Find the first bit cleared to 0 starting from the MSBxwbmpop_weight(): Count the number of 1 bits