Get the bit which is most at the left with dichotomy

Jay

I'm trying to understand how this following code works and I am not able to understand what happens here:

uint32_t mask[5] = { 0xFFFF0000, 0xFF00, 0xF0, 0b1100, 0b10 };
uint32_t shift[5] = { 16, 8, 4, 2, 1 };

char first_bit_left_dichotomy(uint32_t M) {
    char pos = 0;
    char i;
    for (i = 0; i <= 4; i++) {
        if (M & mask[i]) { 
            M = M >> shift[i]; 
            pos += shift[i]; 
        }
    }
    return pos;
}

Indeed, I have two questions please, first of all: if it was a size comparison shouldn't the mask be created in this order?

uint32_t mask[5] = { 0b100, 0b1100, 0xF0, 0xFF00, 0xFFFF0000 };

Thus, what does the program in the for loop please? With my research I understand & and >> and their bitwise behaviours, but what's the trick here because I guess it only and only compares with mask[0] since it's necessary the same size.

junix

The algorithm follows the "divide and conquer" principle by applying a binary search on or bit pattern to figure out what the most significant bit is.

It basically halves the machine word with every cycle. The beauty of this is, that you always calculate MSB within 5 steps, regardless what 32 bit pattern you put in as the binary search has O(log2(n)) characteristics.

Let's pick two extremes to illustrate the behaviour and assume the word 0x00000001 as an input to the algorithm. We would expect it to output 0. What basically happens is:

0x00000001 & 0xFFFF0000 = 0x00000000
-> We don't shift anything, M=0x00000001, pos=0

0x00000001 & 0xFF00 = 0x0000
-> We don't shift anything, M=0x00000001, pos=0

0x00000001 & 0xF0 = 0x00
-> We don't shift anything, M=0x00000001, pos=0

0x00000001 & 0b1100 = 0x00
-> We don't shift anything, M=0x00000001, pos=0

0x00000001 & 0b10 = 0b0
-> We don't shift anything, M=0x00000001, pos=0

So we got our result within 5 steps. Imagine now doing a loop from left to right attempting the same thing: It would take 31 steps to get to the result.

Also the word 0x8FFFFFFF as an input to the algorithm would need 5 steps to get the expected result 31:

0x8FFFFFFF & 0xFFFF0000 = 0x8FFF0000
-> We shift by 16 right, M=0x8FFF, pos=16

0x8FFF & 0xFF00 = 0x8F00
-> We shift by 8 right, M=0x8F, pos=24

0x8F & 0xF0 = 0x80
-> We shift by 4 right, M=0x8, pos=28

0x8 & 0b1100 = 0x8
-> We shift by 2 right, M=0x2, pos=30

0x2 & 0b10 = 0x2
-> We shift by 1 right, M=0x1, pos=31

As you can see, both extremes took us exactly those 5 steps. Thanks to loop unrolling, conditional execution of instructions, etc. this is supposed to run quite fast, at least by far faster than looking for the MSB set from left to right in a loop.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

If we Shift a bit to left and left most becomes 1 then how to calculate?

From Dev

Most efficient way to get seconds left in this hour

From Dev

Get the most significant bit from an 8-bit value

From Dev

Get the most significant bit from an 8-bit value

From Dev

Get the most left X and the most right X from the AmXYChart

From Dev

Get the element which is the most visible on the screen

From Dev

How to get position of right most set bit in C

From Dev

What is the most elegant way to find 16-bit numbers which satisfy some conditions?

From Dev

MySQL: Get the TOP 3 referrers which referrals spend the most money

From Dev

How to get records which matches most to lesser criteria for user search?

From Dev

MySQL: Get the TOP 3 referrers which referrals spend the most money

From Dev

How to get the word which is most repeated in a list of strings?

From Dev

bit mask left shift

From Dev

Which of the following is the most efficient

From Dev

Which is most cache friendly?

From Dev

Which is the most used name?

From Dev

Which is the most used name?

From Dev

How do I get the "left-most 128 bits of a byte array"?

From Dev

set most significant bit in C

From Dev

Which is the most efficient getBytes in Android to get a UTF-16 byte array

From Dev

Which is the most efficient getBytes in Android to get a UTF-16 byte array

From Dev

Using Java Scanner class ,how do I get the most recent number which comes after specific String?

From Dev

List iteration, which is the most efficient?

From Dev

Which trigonometric function is most appropriate?

From Dev

Which user is running the most processes?

From Dev

List iteration, which is the most efficient?

From Dev

Compare formulas, which is most efficient

From Dev

trying to get a 3rd div to "float over" 2 divs which are "float left" and "float right"

From Dev

Method to get windows left open on second monitor which is off back to primary monitor in Win7?

Related Related

  1. 1

    If we Shift a bit to left and left most becomes 1 then how to calculate?

  2. 2

    Most efficient way to get seconds left in this hour

  3. 3

    Get the most significant bit from an 8-bit value

  4. 4

    Get the most significant bit from an 8-bit value

  5. 5

    Get the most left X and the most right X from the AmXYChart

  6. 6

    Get the element which is the most visible on the screen

  7. 7

    How to get position of right most set bit in C

  8. 8

    What is the most elegant way to find 16-bit numbers which satisfy some conditions?

  9. 9

    MySQL: Get the TOP 3 referrers which referrals spend the most money

  10. 10

    How to get records which matches most to lesser criteria for user search?

  11. 11

    MySQL: Get the TOP 3 referrers which referrals spend the most money

  12. 12

    How to get the word which is most repeated in a list of strings?

  13. 13

    bit mask left shift

  14. 14

    Which of the following is the most efficient

  15. 15

    Which is most cache friendly?

  16. 16

    Which is the most used name?

  17. 17

    Which is the most used name?

  18. 18

    How do I get the "left-most 128 bits of a byte array"?

  19. 19

    set most significant bit in C

  20. 20

    Which is the most efficient getBytes in Android to get a UTF-16 byte array

  21. 21

    Which is the most efficient getBytes in Android to get a UTF-16 byte array

  22. 22

    Using Java Scanner class ,how do I get the most recent number which comes after specific String?

  23. 23

    List iteration, which is the most efficient?

  24. 24

    Which trigonometric function is most appropriate?

  25. 25

    Which user is running the most processes?

  26. 26

    List iteration, which is the most efficient?

  27. 27

    Compare formulas, which is most efficient

  28. 28

    trying to get a 3rd div to "float over" 2 divs which are "float left" and "float right"

  29. 29

    Method to get windows left open on second monitor which is off back to primary monitor in Win7?

HotTag

Archive