Easy math puzzle

What is the value of x?

x = 4 & 3

0, 3, 4, or 7?

The answer, apparently, is zero. ‘Four and three make seven,’ so that would seem the obvious answer. I don’t see how x can equal 4 and 3 at the same time. The ‘zero’ answer seems to depend on binary. Three is ‘11’ and four is ‘100’. Answerers are saying that because of this, the answer is zero. But ‘111’ in binary is 7.

Please explain.

In the binary “AND” operation, each column is “1” if and only if both columns of the operands are “1”: otherwise it is zero. So since

011 and
100

don’t have a column where both columns are “1”, 011 AND 100 = 000 = 0. On the other hand, 7 AND 4 =
111
100

would be 100 = 4 because the first column has zeroes in both numbers.

If I give you 4 apples then I have also given you 3 apples. So x = 4. Or 5. Or any number above 3.

This is a “Bitwise And.”
If it was a “Logical And” (&& in C) the answer would be 1 or TRUE.

For x = both 3 and 4 at the same time, the quadratic x^2 -7x +12=0
9 - 21 +12 =0
or
16 - 28+ 12=0

The problem is that an ampersand in normal English construction doesn’t generally stand for a binary ‘AND’ operation. Not to mention, there’s nothing here that suggests that we have anything other than a base-10 ones column. (If we’re not, we could just as easily be in trinary, in which case the AND operation would give us X = (11 AND 10) = 10 = 3.)

Getting back to the problem itself, an ampersand stands in for the English word ‘and.’ Which doesn’t have a clear, unambiguous mapping onto mathematical operations. Especially in the context given. Does it mean X = 4 plus 3? Does it mean X =4, and simultaneously X=3? Does it mean I have at least 4 of something, and I simultaneously have at least 3 of that same something?

So what we have here is a badly designed math puzzle, because it requires more information to solve. We need to know what ‘and’ means, because each of the four proposed answers can be correct, depending on what is meant by ‘&’.

So is it a scripting thing?

Nope. The answer to the quadratic is x = 4 or x = 3.

Well, I don’t think it is a puzzle. Where did you encounter it, if not in C (or other) computer code?

Indeed, if x = 4 and x = 3, then 4 = 3. Subtracting 3 from both sides then leaves 1 = 0.

I agree, but I was answering the question about binary and, not the question about whether this was a mathematical and operation and what base it was in.

I’ll second this question. Where did you run across this equation?

A little context might help a lot.

Yeah, if I put the line
x = 4 & 3;
into a C program, then the value of x will be 0, because C interprets the symbol ‘&’ to mean bitwise-AND. In some other computer context, that symbol might be interpreted differently. And in a non-computer context, it might be interpreted in any number of other ways.

The C interpretation (which might also be shared by some C-like languages) is probably the most common interpretation that would lead to the answer x = 0, but there might be others.

Python console…


In [389]: x=4&3
In [390]: x
Out[390]: 0

The ‘and’ and ‘or’ operations are based on standard conversational meaning. So, consider this sentence:

If Bob and Melinda are both physically pregnant, then let’s go buy some ice cream.

A key component of that sentence is the word “both”. And is an implied “both”. I could write the same sentence without but for the sake of clarity, I’ve included it and, as said, it’s implied by the word “and”.

Bob, as a dude, is unlikely to be physically pregnant. We are not going to go buy ice cream.

And requires both things to be true. In the mathematical/computer world, true is ‘1’ and false is ‘0’.

So we have a requirement that both things be true while looking - column by column - at two lists of 1s and 0s:

100
011

In none of these cases (comparing column by column) have we achieved a “both”. If we have a both - both things are true (1) - then we have a success. Success is also 1. Failure is 0.

We can view the first number (100) like a person with certain attributes:

Bob

  • Is male (1)
  • Is not pregnant (0)
  • Does not want ice cream (0)

Melinda

  • Is not male (0)
  • Is pregnant (1)
  • Does want ice cream (1)

If we go line by line, asking if that line is true for both of them, we’ll get a zero for all cases.

Both:

  • Not both male (0)
  • Not both pregnant (0)
  • Don’t both want ice cream (0)

Whereas, there is also the OR operation. ‘Or’ uses an “either” concept instead of a “both” concept:

Either:

  • At least one of them is male (1)
  • At least one of them is pregnant (1)
  • At least one of them wants ice cream (1)

I too am curious about the context.

To someone who is familiar with bitwise and (especially if they’ve used a computer language where & is the symbol for it), “3 & 4” resulting in 0 is perfectly natural. To someone who isn’t familiar with bitwise and, it’s probably mystifying.

This reminds me of one of those Facebook order of operations memes that were all over the place a while back. Someone posts some mathematical expression that is deliberately unclear and then the internet argues about it for a bit.

e.g.

More of a general programming thing than specifically scripting. In many (most?) languages, you use the symbol “&” as the operator to perform a bitwise AND (but this varies by languages. Most of the major ones I could think of do it with the & operator. I do remember the old 8-bit BASICs, though, used the “AND” keyword for both bitwise and logical AND, and context determined which one was used.)

Since I know from the other thread you have a Mac, you can play around with this by going into terminal and typing “python” to launch a python interpreter. Type “print 4 & 3” and see it will result in “0”. Type “print 7 & 3” and see it will result in “3.” (If you’re further interested in bitwise operators, the equivalent symbol for bitwise OR is | (vertical pipe) and bitwise XOR is ^. Then there’s the bit-shifting ones, but I’ll just leave those alone. But, once again, these vary by language, though those three symbols are common across many of the major languages. This may be important to realize if you dabble in programming, because you might think “print 2^2” should result in 4, as 2 to the power of 2, but it’s actually a bitwise operator in python and C and a bunch of languages, so you’ll result with a 0 instead.)

A meme.

Nailed it.