Finding the next in round-robin scheduling by bit twiddling

前端 未结 9 1875
日久生厌
日久生厌 2021-02-02 02:46

Consider the following problem. You have a bit-string that represents the current scheduled slave in one-hot encoding. For example, \"00000100\" (with the leftmost bit being #7

9条回答
  •  我在风中等你
    2021-02-02 02:56

    Assuming twos complement representation, call your two words mask and current, in C:

    mask_lo = (current << 1) - 1; // the bits to the right and including current
    mask_hi = ~mask_lo;           // the bits to the left of current
                                  // the left bits, otherwise right:
    next = (mask & mask_hi) ? (mask & mask_hi) : (mask & mask_lo);
    return (next & -next);        // the least significant bit set
    

提交回复
热议问题