Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Determine whether a given bit in an integer is set to one
ResourceFunction["BitTest"][n, {k1,k2, …}] returns a list of truth values for each ki. | |
ResourceFunction["BitTest"][n, {k1,k2, …}, op] applies the logical operator op to the list of truth values. |
Display coefficients at powers of 2 in the integer 42:
In[1]:= | ![]() |
Out[1]= | ![]() |
Determine whether the bit at position 3 is set:
In[2]:= | ![]() |
Out[2]= | ![]() |
Test whether bits 0 through 5 are set:
In[3]:= | ![]() |
Out[3]= | ![]() |
Various logical operators can be passed down as a third argument:
In[4]:= | ![]() |
Out[4]= | ![]() |
Test whether a 4-bit register should set a carry flag after performing addition:
In[5]:= | ![]() |
Out[6]= | ![]() |
The following are equivalent:
In[7]:= | ![]() |
Out[7]= | ![]() |
The DataStructure "BitVector" has this function as one of its methods:
In[8]:= | ![]() |
Out[9]= | ![]() |
This work is licensed under a Creative Commons Attribution 4.0 International License