Finding the next in round-robin scheduling by bit twiddling

前端 未结 9 1721
日久生厌
日久生厌 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:55

    Subracting 1 is the essential idea here. It's used to cascade borrows through the bits to find the next task.

    bits_before_current = ~(current-1) & ~current
    bits_after_current = current-1
    todo = (mask & bits_before_current) 
    if todo==0: todo = (mask & bits_after_current) // second part is if we have to wrap around
    next = last_bit_of_todo = todo & -todo
    

    This will use a loop internally though...

提交回复
热议问题