I am learning how to print all the subsets with same length in Javascript. And I saw this solution on w3resource.com:
JavaScript Function: Exercise-21 with Solution
Write a JavaScript function to get all possible subset with a fixed length (for example 2) combinations in an array.
Sample array :
[1, 2, 3]and subset length is 2Expected output :
[[2, 1], [3, 1], [3, 2], [3, 2, 1]]JavaScript Code:
function subset(arra, arra_size) { var result_set = [], result; for(var x = 0; x < Math.pow(2, arra.length); x++) { result = []; i = arra.length - 1; do { if( (x & (1 << i)) !== 0) { result.push(arra[i]); } } while(i--); if( result.length >= arra_size) { result_set.push(result); } } return result_set; }
However I don't understand to logic behind the code especially the line with the bitwise operator. Could anyone explain please?