5

Please read below, as there is an important restriction to this question.

Think of a number as an array of 0's and 1's. The logical operators I want to use are the usual: AND, OR, XOR, NAND, NOT (negation or "!").

To check if TWO numbers a and b are equal, simply do:

NOT(a XOR b)

This will be an array of 1's only if the two numbers are equal.

Thus to check if three numbers are equal I could simply do:

NOT(a XOR b) AND NOT(b XOR c)

However, the restriction is that I cannot write any of the numbers a,b,c more than once. So I need something similar to this:

(a OPERATOR1 b) OPERATOR2 c,

where each of the numbers a,b,c appear exactly once.

I'd be happy to answer questions on the context of this question. Thanks in advance.

wircho
  • 1,431

4 Answers4

1

I'm afraid that Not(a XOR b) AND NOT(b XOR c) is as concise as you're going to get.

It's the most compact expression which will return $1$ if and only if $a = b = c$. All other modes of expressing this with the connectives you list involve multiple appearances of one or more of $a, b, c$.

If you can use BOOLE ($\Leftrightarrow$): then you can limit to one occurrence of each of a, b, c:

$$a \Leftrightarrow b \Leftrightarrow c$$


See for example: Wolfram's elaboration

amWhy
  • 209,954
  • You are missing a parenthesis. – wircho Feb 18 '13 at 16:35
  • About $\Leftrightarrow$: Not really, since this is properly written as $(a\Leftrightarrow b)\mathbf{AND}(b\Leftrightarrow c)$, then you are still using one of them twice. – wircho Feb 18 '13 at 17:04
  • wircho, it is considered a valid (and standard) minimal form. It's expansion involves AND, but then so does XOR: the expansion of XOR involves (a OR b) AND NOT (a AND b) – amWhy Feb 18 '13 at 17:06
  • I know it is right in mathematical language but my point is that you cannot write it like that in any programming language. The expression (a==b==c) is understood as (a==b)==c which just compares c to a boolean value. – wircho Feb 18 '13 at 17:08
  • Thanks for sayin so that i am near 15k. – Mikasa Feb 21 '13 at 13:55
  • @Babak You are so close...by tomorrow, I bet, and well deserved. – amWhy Feb 21 '13 at 13:56
  • @amWhy: Yes I hope so, but I am making some very simple question than others like you and other Masters do here. I imagine myself here as a amateur who are sitting on the shore of a very vast sea and just play with water while you are on a boat in the middle of this stormy sea. ;-) – Mikasa Feb 21 '13 at 14:01
  • The questions you answer aren't always simple...you do a great job, e.g., in group theory (and across the board)...it takes compassion and talent, like you have, to help with simpler questions too, (many of which I answer, as well), because we cannot assume all the tools we may know are known to the OP. So no need to belittle your contributions. You have a helpful, cooperative, and eager-to-help style that I wish more of the "super high rep" users employed regularly! – amWhy Feb 21 '13 at 14:05
  • @BabakS Anyway, I'm glad to see you're doing well, and it's clear you English is getting good! Personally, I'm having a not-so-good day in many not-so-good ways :-/ – amWhy Feb 21 '13 at 14:07
  • @amWhy: Me too. I pray for you to defeat all obsolesces, amWhy. :-) – Mikasa Feb 21 '13 at 14:11
  • @amWhy: New day has come, as Celine songs. 10k + for you. ;-) – Mikasa Feb 22 '13 at 20:24
  • Thanks, @BabakS. I appreciate that. – amWhy Feb 22 '13 at 20:26
  • you could check for a false (a XOR b) OR (b XOR c) too, to make things more elegant. – Zuabros Jul 27 '20 at 20:53
1

Edit: My proof is not valid, as I forgot about $\mathtt{XOR}$. Nevertheless, I will leave it here, it still might be useful for somebody.

Edit 2: As I don't know how to simply fix this issue, I will give a short informal argument based on information theory: with $x_i$ being used only once, we have only 1 bit (the output of some sub-expression) to convey 2 bits of information:

  • whether the previous comparison succeed,
  • whether we are comparing to $0$ or to $1$.

It is possible for $n = 2$, because we don't need to propagate the "previous comparisons succeeded" bit (there is no previous comparisons). If only we could use every $x_i$ twice, it would be possible for any $n$, because we could implement (0).


This is impossible. As different indices/bits of input numbers are treated independently, WLOG we can assume that all the input $x_i \in \{0,1\}$. So we have a sequence $x_i$ and want to construct a logical expression which is equivalent to

$$\left(\forall i.\ x_i = 1\right) \lor \left(\forall i.\ x_i = 0\right), \tag{0}$$

such that every $x_i$ is used at most once. It is doable for $n \leq 1$, but impossible otherwise, i.e. for $n > 1$. Let's assume that a Boolean function $f(x_0,\ldots)$ uses $x_0$ only once, then $f$ is increasing in $x_i$ or $f$ is decreasing in $x$, more formally

$$\forall (x_i)\subset\{0,1\}^n.\ f(0,x_1,\ldots) \leq f(1,x_1,\ldots), \tag{1}$$

or

$$\forall (x_i)\subset\{0,1\}^n.\ f(0,x_1,\ldots) \geq f(1,x_1,\ldots). \tag{2}$$

The reason being, operators $\lor$ and $\land$ are monotonic and if $x_0$ is used only once, then there is a constant number $k_0$ of negations along the way. $\mathtt{NAND}$ is just $\mathtt{NOT} \circ \mathtt{AND}$, and if $k_0$ is even then (1), and for $k_0$ odd we have (2).

By symmetry, it is the same for $x_i$ for any $i$. However, the desired operation $equiv(x_0,x_1,x_2)$ does not fulfill (1) or (2), e.g.

\begin{align} equiv(0,0) &= 1 \\ equiv(1,0) &= 0 \\ equiv(0,1) &= 0 \\ equiv(1,1) &= 1 \end{align}

Concluding, $equiv(x_0,\ldots)$ for $n > 1$ does not fulfill (1) or (2) and thus is not expressible in terms of $\lor, \land,\neg$ with variables being used only once.

I hope it answers your question ;-)

dtldarek
  • 37,381
1

There is no expression of the form $(a *_1 b) *_2 c$ where $a$, $b$ and $c$ are arbitrary bits and $*_1$ and $*_2$ are one of the bitwise operators $and$, $or$, $xor$ or their negations $nand$, $nor$ and $nxor$ that evaluates to true whenever $a = b = c$. I've used a program to test that all possible choices of $*_1$ and $*_2$ will not give the desired output in all cases.

https://gist.github.com/timjb/6587803

0

(a xor b)'.(b xor c)'
= (a'b+b'a)'(b'c+c'b)'
= (a'b+b'a+b'c+c'b)'
= (b(a'+c')+b'(a+c))'
= (b(a.c)'+b'(a'c')')'
= (b xor (a and c))'

tvamsisai
  • 961