Q:

Converting decimal to binary and using it as a filter

So the user enters a number and then gives a map of key value pairs(keys from 0 to n). 
eg. Number = 6
    Map = {{0,a},{1,b},{2,c},{3,d},{4,e},{5,f}}
The problem is to convert number to binary(110 in this case) and print elements from the map which have bit 1 corresponding to its position.
in this case print {0,a} and {1,b} as they correspond to "110"
I converted the number to binary by dividing by two recursively and then traversing the array from end and printing the corresponding value in Map if the bit is 1.
I was asked this during an internship, and for my solution, I was told it was very very inefficient and had high time and space complexity. I was asked to use AND operator to do this efficiently. No more was said and we moved on. I still wonder how to complete this using AND operator. So I would like to know how AND operator should be used here to get the solution

A:

You need to know which bits are set in the given number so that you can print the numbers with the corresponding keys.
Using the bitwise-and operator you can mask out those bits and check whether they are set.
The following is in JS but it's very similar in other languages:

const num = 6;
let mask = 1;
for(let i = 0; i < 8; i++) {
console.log("num (binary rep):\t", ("0".repeat(7) + num.toString(2)).slice(-8))
console.log("mask (binary rep):\t", ("0".repeat(7) + mask.toString(2)).slice(-8))
  console.log(!!(num & mask));
  console.log(i+1, `th bit is${num & mask ? "" : " not"} set!`)
  console.log("*".repeat(20))
  mask = mask << 1;
}

There is a lot of fluff in the above code for presentation purposes but it boils down to the following lines:
if (mask & num)
    console.log("Hurray! Set! Do stuff")
mask = mask << 1

Now, since the keys of your dictionary are all integers starting from 0 and increasing by one, you can incorporate the logic for checking them right into the above loop.

