1

i have a sting with a byte in it ("00001011") and now id like to get a array with all possible combinations of the 1 (acitve) "bits" in it also as a "byte string" so from

var bString = "00001011"; //outgoing string

to a array with all string in it with all possible combinations of this "byte string" like - "00000001", "00000011", "00000010" and so on

is that possible?

thank you in advance

PaintX
  • 25
  • 4
  • your required output is unclear - how many values are you expecting for the example input (I'm guessing 7 different outputs for the example, but not sure) - also, is the order important? why is 0011 before 0010 for instance – Jaromanda X Oct 30 '15 at 10:40
  • the output should be exact how many combinations are possible so here the 7 but also all for this string ""11001011" and so on so for the whole byte – PaintX Oct 30 '15 at 10:49
  • Can you write a full example output? – Yeldar Kurmangaliyev Oct 30 '15 at 10:51
  • so you really want all 8 bit numbers other than 0, so 1 to 255 – Jaromanda X Oct 30 '15 at 10:51

3 Answers3

2
function combinations( input ){
   var number = parseInt( input, 2 );
   var combinations = [];
   var zeroes = (new Array(input.length)).join(0);
   for(var i=1;i<=number;i++){
     if((i&number) == i){ combinations.push( i ) }
   }
   return combinations.map( function(dec){
      return  (zeroes + dec.toString(2)).substr( -zeroes.length-1 );
   });
}

http://jsfiddle.net/jkf7pfxn/3/

console.log( combinations("00001011") );
// ["00000001", "00000010", "00000011", "00001000", "00001001", "00001010", "00001011"]

The idea goes as follows: iterate all numbers from 1 to the input number. If current number AND input number return the current number then both have 1 bits in the same place.

On a smaller number, "0101" (which is 5) it works as follows:

1 & 5 == 1, (0001 & 0101) push 1 to the matches.

2 & 5 == 0, (0010 & 0101) no match.

3 & 5 == 1, (0011 & 0101) no match.

4 & 5 == 4, (0100 & 0101) push 4 to the matches.

5 & 5 == 5, (0101 & 0101) push 5 to the matches.

So the combinations for 0101 are 1 (0001), 2 (0010), 4 (0100) and 5 (0101).

Then there's this little trick to pad numbers with zeroes:

var zeroes = (new Array(input.length)).join(0); // gives a long enough string of zeroes

then

 // convert to base 2, add the zeroas at the beginning, 
 // then return the last n characters using negative value for substring
 return (zeroes + dec.toString(2)).substr( -1 * zeroes.length);
pawel
  • 35,827
  • 7
  • 56
  • 53
  • That's what I thought he wanted - except I think he wants `for(var i=1;i<=number;i++){` - because he did say "7" combinations for the example input – Jaromanda X Oct 30 '15 at 10:55
  • I also think you need `var zeroes = (new Array(input.length + 1)).join(0);` - because your output is 1 digit short : +1 for the fact that your code is adaptable to different input lengths – Jaromanda X Oct 30 '15 at 10:59
  • the outgoing string is missing then all 7 possibilities are in there (all zeros are not needed) – PaintX Oct 30 '15 at 11:06
  • @JaromandaX thanks for your suggestions, included them in my answer. – pawel Oct 30 '15 at 11:09
  • `-1 * zeroes.length + 1` should be `-1 * zeroes.length - 1` or `-1 * (zeroes.length + 1)` or simply `-(zeroes.length + 1)` or `-zeroes.length - 1` – Jaromanda X Oct 30 '15 at 11:19
0

Since 11111111 is 255 so just loop all values and convert them to binary

$(document).ready(function() {
  for (var i = 0; i < 256; i++) {
    $('#core').append('<div>' + dec2bin(i) + '</div>');
  }

  function dec2bin(dec) {
    return ('00000000' + (dec >>> 0).toString(2)).slice(-8);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='core'></div>
Justinas
  • 41,402
  • 5
  • 66
  • 96
0

If you want to enumerate all combinations of binary numbers where 1 can only be in the place of your pattern, you can write a simple recursive function:

var input = "00010111";
var current = [];

function combinations()
{
    if (input.length === current.length)
    {
        var output = current.join('');
        if (parseInt(output, 2) !== 0) // exclude all-zeroes case
            document.body.innerHTML += output + "<br/>";
        return;
    }    
    
    current.push('0'); 
    combinations();
    current.pop();
    
    if (input[current.length - 1] === '1') 
    {
        current.push('1');
        combinations();
        current.pop();
    }
}

combinations();

This algorithm works well for input of any length.
Although it is a recursion, it has a linear time complexity.

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101