最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Pull array values associated with bitmask - Stack Overflow

matteradmin3PV0评论

I have a simple bitmask, 3 ("011" in base 2) which denotes that I should extract array[0] and array[1] but not array[2]

What is an efficient way to do this?

Ultimately, I'm generating a new array with values that passed a .filter

Something like this:

var bitmask = 37, // "100101"
    array = ["a", "b", "c", "d", "e", "f"];

var array2 = array.filter((value, index) => {
    // do something with bitmask and index to return true
});

// array2 should be ["a", "c", "f"];

I have a simple bitmask, 3 ("011" in base 2) which denotes that I should extract array[0] and array[1] but not array[2]

What is an efficient way to do this?

Ultimately, I'm generating a new array with values that passed a .filter

Something like this:

var bitmask = 37, // "100101"
    array = ["a", "b", "c", "d", "e", "f"];

var array2 = array.filter((value, index) => {
    // do something with bitmask and index to return true
});

// array2 should be ["a", "c", "f"];
Share edited Jun 30, 2016 at 2:15 neaumusic asked Jun 30, 2016 at 2:10 neaumusicneaumusic 10.5k11 gold badges59 silver badges86 bronze badges 6
  • That's not a bitmask, it's a string. – RJM Commented Jun 30, 2016 at 2:14
  • you're right, but you know what i mean, it's actually a number – neaumusic Commented Jun 30, 2016 at 2:14
  • Actually covering it to binary then a string would be usefully, that way you can iterate through it and check for "1". – Spencer Wieczorek Commented Jun 30, 2016 at 2:16
  • the thing is, I'm iterating from 000001 to 111111 so that i can get all possible selections to fill up to 6 slots, not sure i want to loop through a string as well as the array – neaumusic Commented Jun 30, 2016 at 2:18
  • Isn't that supposed to return ['a', 'd', 'f']? Or I get your question wrong.. And what if the bitmask length is higher than the array length? – choz Commented Jun 30, 2016 at 2:20
 |  Show 1 more ment

3 Answers 3

Reset to default 10

Expanding on your original example you can do this:

var bitmask = 37, // "100101"
    array = ["a", "b", "c", "d", "e", "f"];

var array2 = array.filter((value, index) => {
    // do something with bitmask and index to return true
    return bitmask & (1 << index);
});

// array2 should be ["a", "c", "f"];
console.log(array2);

var bitmask = 5, idx=0;
// Loop till bitmask reach 0, works when bitmask >= 0
// If you want to sure instead of implicit type converting (from number to boolean)
// Just change it to while(bitmask >= 0)
while(bitmask){
   // If the rightmost bit is 1, take the array[index]
   if(bitmask & 1) console.log("take arr["+idx+"]");
   // Shift right by 1 bit, say 5 = 101,  this will make the number bee 2 = 10
   bitmask>>=1; 
   // Increase the index 
   idx++;
}

Using your own example, here is the code works:

var bitmask = 37, // "100101"
    array = ["a", "b", "c", "d", "e", "f"],
    idx = 0;
var array2 = [];
while(bitmask){
   if(bitmask & 1) array2.push(array[idx]);
   bitmask>>=1; 
   idx++;
}

Simply use some bit operation to loop it. As it is looping bit by bit, I think it is the fastest you can get

One way to do this is cast your number into a binary string, then check if the index of the bitmask is "1" inside your filter.

    var bitmask = (37).toString(2), // "100101"
        array = ["a", "b", "c", "d", "e", "f"];
    
    var array2 = array.filter((value, index) => {
        if(bitmask[index] == "1") return value;
    });
    
    console.log(array2);

Post a comment

comment list (0)

  1. No comments so far