Bit Pattern Arguments in Python

You have probably come across functions or methods with a number of Boolean or yes/no options combined into a single parameter, the arguments being assembled from constants OR'ed together. A call to such a function or method looks something like this.
In this article I’ll explain how this works as well as presenting one of the possible ways of implementing the pattern in Python.
Using Bits as Boolean Values
As you probably know every bit in, for example, an integer is in effect a Boolean specifying whether the value represented by that specific bit is included in the overall value of the integer. The following example illustrates the binary representation of the decimal number 170, the total of the bit values for which the bit is 1 or true.
There is no reason the individual bits should be interpreted in this way and they can just as easily be used for any Boolean values, for example a set of options passed as a function argument which is the subject of this article.
For this project I will create a sample function with these eight options represented by the following values, shown in both decimal and binary.
The actual names vaguely represent options you might need for a print function but are of no significance. The function does nothing but extract and print out the True/False values of each bit, and the purpose of this article is to show how to do this so that you can apply the pattern to your own requirements.
Let’s assume the calling code wants to specify FOOTER, TITLE, BYLINE and CENTRE_VERT. The pattern of bits for this combination is as follows. (This is decimal 170 as in the first example but that’s irrelevant here as we are not using the bits to represent an integer.)
What is needed therefore is a function with an appropriately named parameter which in this particular case will have an argument value of the integer represented by the above pattern of bits.
There are two separate problems here:
How to code the function to separate out the bits passed as an argument
How to assemble the correct pattern of bits to use when calling the function
Full descriptions of these two tasks are best left to the commentary on the actual code but I’ll briefly describe the principles.
Firstly, given an integer how do we code a function to find out whether each particular bit is 1? The answer is to AND it with another integer with only that bit set. The result is another integer which is either 0 or > 0 which we interpret as False or True for that particular bit. These tables illustrates the process for the TITLE bit 8 shown in bold.
Each bit in the result is only 1 if both the input bits are 1. This of course is why the operation is called AND. In the first example the bit is set so the result is > 0, specifying “yes, show the title”. In the second example the bit is not set so the result = 0 which is interpreted as “no, don’t show the title”. It’s important to note that the other bits of the argument have no effect on any particular result.
The second problem is for the calling code to assemble a pattern of bits corresponding to the required options to pass to the function. This is done by OR’ing integers values with just the required bits set. The following table shows patterns of bits representing three options and the result of them being OR’ed, this being a combination of the 1s in the three inputs.
All of the above is rather abstract and I have deliberately kept the description independent of any programming language. I have been unable to find when this pattern was first used and in which language but I suspect it dates back to the very dawn of computers and programming when every bit was a precious resource. If you know please leave a comment.
Why?
There is no reason why you couldn’t code a function with individual Boolean parameters. If there are maybe two or three this is probably the best option. The reasons for scrunching a number of Boolean values into a pattern of bits are twofold:
The function signature is more compact and simpler to both code and use
It saves a small amount of memory
The first reason is universal and any pattern which makes code easier to write and read is all to the good.
The second reason is open to debate. If you are coding something where this is an important consideration, say something running huge amounts of function calls, then you probably aren’t using Python. However, as I mentioned above this technique is pretty much language agnostic so can be beneficial in C, Go or whatever.
The Project
This project consists of the following files:
bitoptions.py
bitoptionshemo.py
The files can be cloned or downloaded from the GitHub repository.
The Code
When developing this code I realised there are a number of different approaches to implementing the required functionality. After trying a few things out the solution below appealed to me the most but you might think up a slightly or radically different solution. Variety is the spice of life. Different strokes for different folks.
Enough mottos, let’s look at the code. This is bitoptions.py which contains a function consuming a bit pattern of options.
Basic integer values representing the various options, specifically 1, 2, 4 etc., would be pretty difficult to work with so it’s necessary to assign them meaningful names. The approach I have used is to create a namedtuple called Options with an instance named options. These can be used in both the function consuming the options and calling code.
The dosomething function has a single parameter, opts*. This is firstly printed as-is, formatted as binary. Next a for loop iterates through each bit - the 2**e gives us 1, 2, 4 etc. - before printing the bit values as decimal and the bit’s Boolean value. Note the use of the & (AND) operator as described above.
*I stupidly called it options originally and spent ages trying to debug what was a simple name clash. 🤦♂️
The rest of the code might seem repetitive and cumbersome but in real situations you are likely to have to check each option individually and run differing blocks of code for each. I therefore think the code is realistic even though here I have just printed appropriate pieces of text.
There’s the possibility of some very slight abstraction of the bit checking by moving the AND’ing to a separate utility function, something like this.
Is it worthwhile? I’m really not sure!
Now we just need to call dosomething to see the code in action. This is bitoptionsdemo.py.
Running the Program
Run the demo code like this.
python3 bitoptionsdemo.py
This is the end result.
As I mentioned this is just my take on the problem. Please let me know if you can think of or have used a different method.
For updates and random ramblings please follow me on Bluesky.
No AI was used in creating the code, text or images in this article.












