tritwise.js
function dyadic_pref_op(pref, input1, input2, input_width) {
  if (pref < -13 || pref > 13) throw new Error('dyadic_pref_op('+pref+'): out of range iii-111');
  var bt_pref = pad(3, n2bts(pref), '0');
  if (bt_pref.indexOf('i') === -1 || bt_pref.indexOf('0') === -1 || bt_pref.indexOf('1') === -1)
    throw new Error('dyadic_pref_op('+pref+'): preference must contain all digits i,0,1');

  var input1_bt = pad(input_width, n2bts(input1), '0');
  var input2_bt = pad(input_width, n2bts(input2), '0');

  var output_bt = '';

  for (var i = 0; i < input_width; ++i) {
    var a = input1_bt.charAt(i);
    var b = input2_bt.charAt(i);

    if (bt_pref.indexOf(a) < bt_pref.indexOf(b)) {
      // a has a higher preference than b (comes earlier in preference list), choose a
      output_bt += a;
    } else {
      // otherwise choose b (preference functions always choose one of the inputs)
      output_bt += b;
    }
  }

  var output = bts2n(output_bt);

  return output;
}
function dyadic_pref_op(pref, input1, input2, input_width) {
  if (pref < -13 || pref > 13) throw new Error('dyadic_pref_op('+pref+'): out of range iii-111');
  var bt_pref = pad(3, n2bts(pref), '0');
  if (bt_pref.indexOf('i') === -1 || bt_pref.indexOf('0') === -1 || bt_pref.indexOf('1') === -1)
    throw new Error('dyadic_pref_op('+pref+'): preference must contain all digits i,0,1');

  var input1_bt = pad(input_width, n2bts(input1), '0');
  var input2_bt = pad(input_width, n2bts(input2), '0');

  var output_bt = '';

  for (var i = 0; i < input_width; ++i) {
    var a = input1_bt.charAt(i);
    var b = input2_bt.charAt(i);

    if (bt_pref.indexOf(a) < bt_pref.indexOf(b)) {
      // a has a higher preference than b (comes earlier in preference list), choose a
      output_bt += a;
    } else {
      // otherwise choose b (preference functions always choose one of the inputs)
      output_bt += b;
    }
  }

  var output = bts2n(output_bt);

  return output;
}
function dyadic_pref_op ( pref , input1 , input2 , input_width ) { if ( pref < - 13 || pref > 13 ) throw new Error ( 'dyadic_pref_op(' + pref + '): out of range iii-111' ) ; var bt_pref = pad ( 3 , n2bts ( pref ) , '0' ) ; if ( bt_pref . indexOf ( 'i' ) === - 1 || bt_pref . indexOf ( '0' ) === - 1 || bt_pref . indexOf ( '1' ) === - 1 ) throw new Error ( 'dyadic_pref_op(' + pref + '): preference must contain all digits i,0,1' ) ; var input1_bt = pad ( input_width , n2bts ( input1 ) , '0' ) ; var input2_bt = pad ( input_width , n2bts ( input2 ) , '0' ) ; var output_bt = '' ; for ( var i = 0 ; i < input_width ; ++ i ) { var a = input1_bt . charAt ( i ) ; var b = input2_bt . charAt ( i ) ; if ( bt_pref . indexOf ( a ) < bt_pref . indexOf ( b ) ) { // a has a higher preference than b (comes earlier in preference list), choose a output_bt += a ; } else { // otherwise choose b (preference functions always choose one of the inputs) output_bt += b ; } } var output = bts2n ( output_bt ) ; return output ; }
Dyadic preference functions
Dyadic preference functions