javascript - Flag bit computation and detection - Stack Overflow

admin2025-03-31  7

In some code I'm working on I should take care of ten independent parameters which can take one of two values (0 or 1). This creates 2^10 distinct conditions. Some of the conditions never occur and can be left out, but those which do occur are still A LOT and making a switch to handle all cases is insane.

I want to use 10 if statements instead of a huge switch. For this I know I should use flag bits, or rather flag bytes as the language is javascript and its easier to work with a 10 byte string with to represent a 10-bit binary.

Now, my problem is, I don't know how to implement this. I have seen this used in APIs where multiple-selectable options are exposed with numbers 1, 2, 4, 8, ... , n^(n-1) which are decimal equivalents of 1, 10, 100, 1000, etc. in binary. So if we make call like bar = foo(7), bar will be an object with whatever options the three rightmost flags enable.

I can convert the decimal number into binary and in each if statement check to see if the corresponding digit is set or not. But I wonder, is there a way to determine the n-th digit of a decimal number is zero or one in binary form, without actually doing the conversion?

In some code I'm working on I should take care of ten independent parameters which can take one of two values (0 or 1). This creates 2^10 distinct conditions. Some of the conditions never occur and can be left out, but those which do occur are still A LOT and making a switch to handle all cases is insane.

I want to use 10 if statements instead of a huge switch. For this I know I should use flag bits, or rather flag bytes as the language is javascript and its easier to work with a 10 byte string with to represent a 10-bit binary.

Now, my problem is, I don't know how to implement this. I have seen this used in APIs where multiple-selectable options are exposed with numbers 1, 2, 4, 8, ... , n^(n-1) which are decimal equivalents of 1, 10, 100, 1000, etc. in binary. So if we make call like bar = foo(7), bar will be an object with whatever options the three rightmost flags enable.

I can convert the decimal number into binary and in each if statement check to see if the corresponding digit is set or not. But I wonder, is there a way to determine the n-th digit of a decimal number is zero or one in binary form, without actually doing the conversion?

Share Improve this question edited Apr 25, 2010 at 7:20 Oded 500k102 gold badges893 silver badges1k bronze badges asked Apr 25, 2010 at 7:18 Majid FouladpourMajid Fouladpour 30.3k21 gold badges78 silver badges129 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

Just use a bitwise-and. In C/C++, this would be:

if (flags & 1) {
    // Bit zero is set.
}
if (flags & 2) {
    // Bit one is set.
}
if (flags & 4) {
    // Bit two is set.
}
...

For production goodness, use symbolic names for the flag masks instead of the magic numbers, 1, 2, 4, 8, etc.

If the flags are homogeneous in some way (e.g., they represent ten spatial dimensions in some geometry problem) and the code to handle each case is the same, you can use a loop:

for (int f = 0; f < 10; ++f) {
    if (flags & (1 << f)) {
        // Bit f is set.
    }
}

You can use a bitwise and:

10 & 2^1 is true because 10 = 1010b 
                                ^ 1
 8 & 2^1 is false because 8 = 1000b 
                                ^ 0
10 & 2^3 is true because 10 = 1010b 
                              ^ 1

You could get a number that has the n-th bit set and AND it with your number. If the result is zero your number didn't have the bit set. Otherwise, it did. Look here, also.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1743352664a212999.html

最新回复(0)