Function Repository Resource:

BitBooleanFunction

Source Notebook

Apply a Boolean function to corresponding bits in integers

Contributed by: Stephen Wolfram

ResourceFunction["BitBooleanFunction"][k][x,y]

computes the kth bitwise Boolean function of the integers x and y.

ResourceFunction["BitBooleanFunction"][k,n][x1,,xn]

computes the kth n-input bitwise Boolean function of the integers xi.

Details

ResourceFunction["BitBooleanFunction"] is a generalization of BitAnd, BitXor, etc.

Examples

Basic Examples (2) 

Compute the sixth bitwise Boolean function:

In[1]:=
ResourceFunction["BitBooleanFunction"][6][123, 456]
Out[1]=

This corresponds to computing a bitwise XOR:

In[2]:=
BitXor[123, 456]
Out[2]=

Scope (1) 

Compute the 150th bitwise Boolean function with three inputs:

In[3]:=
ResourceFunction["BitBooleanFunction"][150, 3][123, 456, 789]
Out[3]=

Applications (3) 

Plot of a Boolean function:

In[4]:=
ListLinePlot[
 Table[ResourceFunction["BitBooleanFunction"][1, 2][i, 2 i], {i, 64}]]
Out[4]=

A 2D plot of Boolean function versus Mod[i, j]:

In[5]:=
ArrayPlot[
 Table[Boole[
   ResourceFunction["BitBooleanFunction"][4, 2][i, j] == Mod[i, j]], {i, 64}, {j, 64}]]
Out[5]=

A 3D plot of a Boolean function:

In[6]:=
Graphics3D[
 Table[Cuboid[{i, j, ResourceFunction["BitBooleanFunction"][1, 2][i, 2 j]}], {i, 31}, {j, 31}]]
Out[6]=

Properties and Relations (1) 

Recover the index of a bitwise Boolean function:

In[7]:=
With[{k = 197, n = 5}, FromDigits[
  ResourceFunction["BitBooleanFunction"][k, n] @@@ Tuples[{1, 0}, n], 2]]
Out[7]=

Neat Examples (1) 

BitBooleanFunction corresponding to BitAnd:

In[8]:=
ArrayPlot[Array[ResourceFunction["BitBooleanFunction"][8], {63, 63}]]
Out[8]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.1 – 13 September 2021
  • 1.0.0 – 04 December 2018

License Information