1

I got the following code using bit operation from here:

lis = Complement[Range[0, 15], {7, 14, 15}]
    ans = Tuples[lis, 3] // 
       Select[#, 
         BitAnd[#1, #2, #3] == 0 && BitAnd[4 #1, 2 #2, #3] == 0 && 
             BitAnd[#1, 2 #2, 4 #3] == 0 & @@ # &] &;
    nums = Length@ans
    GroupBy[ans, 
        Total@Flatten@IntegerDigits[#, 2] &] // #[Max[Keys[#]]] & // 
      IntegerDigits[#, 2, 4] & // TableForm /@ # &

But I'm not familiar with bit operations. How can I understand the meaning of this code and find more examples of bit operations?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
  • 1
    This code treats four-bit integers as arrays of four Boolean values (represented by binary digits 0 and 1). The selection code filters out cases where there's a binary 1-digit on the same, incrementing or decrementing position of the binary representations (or the array positions) of tuple values using the BitAnd operation (multiplication by a power of two acts as a position shifter). – kirma Mar 15 '20 at 06:46
  • @kirma Thank you very much for your comment, which makes me understand the operation mechanism of this code. – A little mouse on the pampas Mar 15 '20 at 07:14
  • 1
    Copied the comment to an answer, as it seems to resolve the issue... – kirma Mar 15 '20 at 09:01

1 Answers1

3

This code treats four-bit integers as arrays of four Boolean values (represented by binary digits 0 and 1). The selection code filters out cases where there's a binary 1-digit on the same, incrementing or decrementing position of the binary representations (or the array positions) of tuple values using the BitAnd operation (multiplication by a power of two acts as a position shifter).

kirma
  • 19,056
  • 1
  • 51
  • 93