Chained Binary Operations

Here's a challenge involving truth tables and binary operations. For this specific challenge, your task is to use the following table of operations:

Binary operation table.

To create a function that takes in two inputs f(p,a), where p is the initial truthy/falsy value and a is a list of tuples representing multiple values of q and the operations to be performed. For instance, f(T,[[T,0],[T,1]]) would return ContradictionOP(T,T)=F. You would then chain the output of this result into the next operation, which would be LogicalNOR(F,T)=F, where the 2nd p is the previous output of F. The final result being false. A better breakdown of what each operation actually is is described below:

Ordinal operator descriptors.

The first part should be rather simplistic in nature, as the operators are arranged in an increasing binary pattern (hint, hint). However, recursing through the additional inputs to chain binary operations is probably the harder part to accomplish.



Extrapolated Example:



  • You MUST use the order specified for the operations as stated above.
  • The input format for a can be a 2D array, array of tuples, command line pairwise arguments or any other representation of pairs of boolean/operations. Could even be a single array that you parse pairwise.
  • You may use 0 for false and 1 for true, it is not acceptable to reverse the two.
  • The output should be simply a truthy or falsy value.
  • No loopholes, this is code-golf, standard CG rules apply.

Example Single Iteration Algorithm:

    op=Integer.toBinaryString(o);     // Calculate the binary OP string (15=1111)
    binaryIndex="$p$q"                // Calculate binary combination of p/q (T/F=10)
    i=Integer.parseInt(binaryIndex,2) // Convert to decimal index (T/F=2)
    op.charAt(i)                      // Get the binary char at the calculated index (T/F/15=1).
f(1,0,15)                             // "1"​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
  • \$\begingroup\$ Can I accept e.g. f([T,[F,15],[T,0],[F,6]])? \$\endgroup\$
    – Neil
    Oct 28, 2016 at 19:01
  • \$\begingroup\$ @Neil, yeah, sure. That's fine. As long as p is distinct then you have pairs of operators. Can also be[true,[false,15],...]. Whatever a boolean is in your language. You do not have to use the character F or T. I was just using that to represent a boolean object. \$\endgroup\$ Oct 28, 2016 at 19:06
  • \$\begingroup\$ This falls just short of my threshold for voting to close as unclear what you're asking, but only just. Is there any good reason for referring to the same operation as F in the first table, FalsyOp in the first example, and Contradiction in the second table? Using the same name consistently would make the question a whole lot more intelligible. \$\endgroup\$ Oct 28, 2016 at 20:32
  • \$\begingroup\$ The ordinals are in the second image for each operation. A ContradictionOP (FalsyOP) returns false regardless of inputs. Look at the first table for all PQ, at ordinal 0 it should return false. Just like for ordinal 6 (XOR) it should return 0, 1, 1 or 0 depending on the PQ. Its actually quite simple if you read the provided code snippet and connect a few dots. Each operation's results can be accessed using the binary value of p+q and the binary operation code. \$\endgroup\$ Oct 28, 2016 at 20:47
  • \$\begingroup\$ @PeterTaylor sorry... Its one of those things seeming complicated, but is quite simple due to a few factors. \$\endgroup\$ Oct 28, 2016 at 22:29

2 Answers 2


JavaScript (ES6), 34 bytes


Accepts true/false but prefers 1/0 format.


CJam (15 bytes)


Online demo. This is an anonymous block (function) which takes the two values on the stack and leaves the result on the stack.


{         e# Define a block:
  {       e#   For each pair in the array...
    +)2@  e#     Prepend the accumulator, pop the op, push a 2, and rot 3
          e#     Stack is now: op 2 [accum arg]
    2b#   e#     Find 2**(2*accum + arg)
    /1&   e#     Test the corresponding bit of op, setting it as the new accum
  • \$\begingroup\$ I like it! Nice job. I should add the rest of the OPs as test cases too. \$\endgroup\$ Oct 28, 2016 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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