1
$\begingroup$

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?

$\endgroup$
  • 1
    $\begingroup$ 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). $\endgroup$ – kirma Mar 15 at 6:46
  • $\begingroup$ @kirma Thank you very much for your comment, which makes me understand the operation mechanism of this code. $\endgroup$ – A little mouse on the pampas Mar 15 at 7:14
  • 1
    $\begingroup$ Copied the comment to an answer, as it seems to resolve the issue... $\endgroup$ – kirma Mar 15 at 9:01
3
$\begingroup$

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).

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.