MathGroup Archive 2013

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: BitAnd[True,False]

  • To: mathgroup at smc.vnet.net
  • Subject: [mg131737] Re: BitAnd[True,False]
  • From: John Fultz <jfultz at wolfram.com>
  • Date: Thu, 26 Sep 2013 03:43:24 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-outx@smc.vnet.net
  • Delivered-to: mathgroup-newsendx@smc.vnet.net
  • References: <20130925063645.E297C6A74@smc.vnet.net>

I'm not sure why you might have expected this to work. It wouldn't occur to me at all that bitwise operations would have anything to do with logical truth tables (unless those tables were expressed in values of 0 and 1).  I assume you've since figured out that you should be using And[], Or[], etc.  But let's figure out why BitAnd seemed to "half succeed" per your expectation.

Taking the standalone examples:

In[1]:= BitAnd[True, True]

Out[1]= True

In[2]:= BitAnd[False, False]

Out[2]= False

In[3]:= BitAnd[False, True]

Out[3]= BitAnd[False, True]


Out[3] makes sense; since True and False aren't integers, it doesn't know what to do.  That Out[1] and Out[2] behave the way they do explains the result of BooleanTable, but what explains Out[1] and Out[2]?  Try this:

In[4]:= BitAnd[x, x]

Out[4]= x

And it turns out that this behavior is documented.  If we look at the documentation for BitAnd, we'll see that the first example in the "Generalizations and Extensions" section indicates that basic symbolic simplifications are done automatically, noting the specific example of:

In[1]:= BitAnd[x, y, y, x]

Out[1]= BitAnd[x, y]

among others.  So, in the case of BitAnd[x, x], the result would be x for any integer.  For the symbols True and False, Mathematica is treating them no differently than any other unassigned symbol.

Furthermore, if you're intrigued by the fact that both the BitAnd[] entries in your truth table have {False, True} rather than one of them having {True, False}, we should note that another example documents this fact.  Under "Properties & Relations" is a note that BitAnd is Orderless with the example:

In[1]:= BitAnd[y, x]

Out[1]= BitAnd[x, y]

That, of course, is very different from And[], for which much user code intimately depends upon the fact that it's an ordered operation.

Sincerely,

John Fultz
jfultz at wolfram.com
User Interface Group
Wolfram Research, Inc.



On Sep 25, 2013, at 1:36 AM, Alan <alan.isaac at gmail.com> wrote:

> I'd hoped BitAnd would work on Boolean Lists so I gave it a try.  If it had simply failed completely I'd be disappointed but would understand.  But oddly, it half succeeds.  Why? BitOr fails the same way, and BitNot always fails.
>
> I just want to understand these results.  I know there are ways to get the output I want.
>
> Thanks,
> Alan Isaac
> (using Mathematica 9)
>
> In[77]:= BooleanTable[{p,q,BitAnd[p,q]},{p,q}]//TableForm
> Out[77]//TableForm=
> True	True	True
> True	False	BitAnd[False,True]
> False	True	BitAnd[False,True]
> False	False	False
>
>




  • Prev by Date: Re: Creating a PDF graphic with Mathematica
  • Next by Date: Re: Quantity
  • Previous by thread: BitAnd[True,False]
  • Next by thread: Re: BitAnd[True,False]