Jordon Huffman
Jordon Huffman

Reputation: 45

Mathematica Generate Binary Numbers with Locked Bits

I have a very specific Mathematica question. I am trying to generate all the binary numbers around certain 'locked' bits. I am using a list of string values to denote which bits are locked e.g. {"U","U,"L","U"}, where U is an "unlocked" mutable bit and L is a "locked" immutable bit. I start with a temporary list of random binary numbers that have been formatted to the previous list e.g. {0, 1, 1, 0}, where the 1 is the locked bit. I need to find all the remaining binary numbers where the 1 bit is constant. I've approached this problem recursively, iteratively, and with a combination of both with no results. This is for research I am doing at my university.

I am building a list of base 10 forms of the binary numbers. I realize that this code is completely wrong. This is just one attempt.

    Do[
      If[bits[[pos]] == "U", 
        AppendTo[returnList, myFunction[bits, temp, pos, returnList]]; ],
    {pos, 8, 1}]

    myFunction[bits_, bin_, pos_, rList_] :=
      Module[{binary = bin, current = Length[bin], returnList = rList},
        If[pos == current,
          Return[returnList],
          If[bits[[current]] == "U",
          (*If true*)
            If[! MemberQ[returnList, FromDigits[binary, 2]],
            (*If true*)
              AppendTo[returnList, FromDigits[binary, 2]];
              binary[[current]] = Abs[binary[[current]] - 1],
            (*If false*)
              binary[[current]] = 0;
              current = current - 1]; ,
          (*If false*)
            current = current - 1];
          returnList = myFunction[bits, binary, pos, returnList];  
        Return[returnList]]]

Upvotes: 3

Views: 263

Answers (3)

agentp
agentp

Reputation: 6989

in = IntegerDigits[Round[ Pi 10^9 ], 2];
mask = RandomSample[ConstantArray["L", 28]~Join~ConstantArray["U", 4],32];

subs[in_, mask_] := Module[ {p = Position[mask, "U"]} ,
       ReplacePart[in, Rule @@@ Transpose[{p, #}]] & /@ 
          Tuples[{0, 1}, Length@p]]
subs[in, mask]

{{1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0}, {1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0}, {1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0}, {1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0}, ...

 FromDigits[#, 2] & /@ % 

{3108030026, 3108030030, 3108038218, 3108038222, 3108095562, 3108095566, 3108103754, 3108103758, 3141584458, 3141584462, 3141592650, 3141592654, 3141649994, 3141649998, 3141658186, 3141658190}

Upvotes: 1

Edmund
Edmund

Reputation: 528

You can use Tuples and Fold to generate only bit sets that you are interested in.

bits = {"U", "U", "L", "U"};

Fold[
  Function[{running, next}, 
    Insert[running, 1, next]], #, Position[bits, "L"]] & /@ Tuples[{0, 1}, Count["U"]@bits]

(*
{{0, 0, 1, 0}, {0, 0, 1, 1}, {0, 1, 1, 0}, {0, 1, 1, 1}, 
 {1, 0, 1, 0}, {1, 0, 1, 1}, {1, 1, 1, 0}, {1, 1, 1, 1}}
*)

Hope this helps.

Upvotes: 2

Chris Degnen
Chris Degnen

Reputation: 8655

myFunction[bits_] := Module[{length, num, range, all, pattern},
  length = Length[bits];
  num = 2^length;
  range = Range[0, num - 1]; 
  all = PadLeft[IntegerDigits[#, 2], length] & /@ range;
  pattern = bits /. {"U" -> _, "L" -> 1};
  Cases[all, pattern]]

bits = {"U", "U", "L", "U"};

myFunction[bits]
{{0, 0, 1, 0}, {0, 0, 1, 1}, {0, 1, 1, 0}, {0, 1, 1, 1},
 {1, 0, 1, 0}, {1, 0, 1, 1}, {1, 1, 1, 0}, {1, 1, 1, 1}}

Upvotes: 1

Related Questions