Bit Manipulation using Templates

published: July 23, 2019 — last modified: October 23, 2025

Did you read my article about C++ templates for embedded code? You learned how to use function templates. This post adds a practical example to this topic.

Bit Manipulation

You may be familiar with bit manipulation code like this:

void setup() {
  DDRB |= 0b00100000u;
  PORTB &= ~0b00100000u;
}

For simplicity, you like to select single bits using its index number.

void setup() {
  DDRB |= (1 << 5);
  PORTB &= ~(1 << 5);
}

This is a common request. Embedded libraries often implement a solution using macros 😞, like this one from the AVR toolchain:

#define _BV(bit) (1 << (bit))

You can use it to simplify your code like this:

void setup() {
  DDRB |= _BV(5);
  PORTB &= ~_BV(5);
}

These macros will clutter the global namespace and are not type-safe. I demonstrate the problem with this example:

#include <iostream>

#define _BV(bit) (1 << (bit))

int main() {
    for (int i = 0; i < 64; ++i) {
        std::cout << i << ": " << sizeof(_BV(i)) << " / " << _BV(i) << std::endl;
    }
    return 0;
}

This is regular C++ code you compile like this:

> c++ example.cpp -o example

If you run this program, which output do you expect?

Think...

Click on "Result" to reveal the output.
0: 4 / 1
1: 4 / 2
2: 4 / 4
3: 4 / 8
4: 4 / 16
5: 4 / 32
6: 4 / 64
7: 4 / 128
8: 4 / 256
9: 4 / 512
10: 4 / 1024
11: 4 / 2048
12: 4 / 4096
13: 4 / 8192
14: 4 / 16384
15: 4 / 32768
16: 4 / 65536
17: 4 / 131072
18: 4 / 262144
19: 4 / 524288
20: 4 / 1048576
21: 4 / 2097152
22: 4 / 4194304
23: 4 / 8388608
24: 4 / 16777216
25: 4 / 33554432
26: 4 / 67108864
27: 4 / 134217728
28: 4 / 268435456
29: 4 / 536870912
30: 4 / 1073741824
31: 4 / -2147483648
32: 4 / 1
33: 4 / 2
34: 4 / 4
35: 4 / 8
36: 4 / 16
37: 4 / 32
38: 4 / 64
39: 4 / 128
40: 4 / 256
41: 4 / 512
42: 4 / 1024
43: 4 / 2048
44: 4 / 4096
45: 4 / 8192
46: 4 / 16384
47: 4 / 32768
48: 4 / 65536
49: 4 / 131072
50: 4 / 262144
51: 4 / 524288
52: 4 / 1048576
53: 4 / 2097152
54: 4 / 4194304
55: 4 / 8388608
56: 4 / 16777216
57: 4 / 33554432
58: 4 / 67108864
59: 4 / 134217728
60: 4 / 268435456
61: 4 / 536870912
62: 4 / 1073741824
63: 4 / -2147483648

You always get a 32-bit signed integer from _BV(), therefore you see issues starting at bit index 31. If you use the macro correctly, the negative numbers are no problem, but if you work with 64-bit values the results are unexpected.

Templates

Let us write safer code with function templates for a pure C++ solution.

template<uint8_t bitIndex, typename Type>
constexpr Type oneBit() {
  return static_cast<Type>(1) << bitIndex;
}

This function template will not only give us a bit from an index but also requires the expected type. Knowing the type is important to prevent invalid index values and unexpected results.

(You will learn static assertions to implement additional checks at a later time.)

Use the new function template like this:

void setup() {
  DDRB |= oneBit<5, uint8_t>();
  PORTB &= ~oneBit<5, uint8_t>();
}

This is safer code, but looks ugly. The type uint8_t is repetitive and the compiler could deduce it from the variable. Also, the bitwise assignment operators |= and &= ~X maybe familiar to you, but they are not well readable.

Let us introduce additional function templates to name the two operations.

template<uint8_t bitIndex, typename Type>
inline void setBit(Type &value) {
  value |= oneBit<bitIndex, Type>();
}

template<uint8_t bitIndex, typename Type>
inline void clearBit(Type &value) {
  value &= ~(oneBit<bitIndex, Type>());
}

Now, you can write code like this:

void setup() {
  setBit<5>(DDRB);
  clearBit<5>(PORTB);
}

We only pass the bit index to the function template, the type is deduced from the variable. This works, because we declared template parameter which can be guessed by the compiler at the end of the parameter list.

Compiled Firmware

Let us compile the example code for Arduino Uno and check the results:

 1aa:	25 9a       	sbi	0x04, 5	; 4
 1ac:	2d 98       	cbi	0x05, 5	; 5

This is all we got — It is the smallest possible code for the bit operations on these IO registers.

Exercises

  • Implement the bit manipulation using regular functions; without templates and macros. Do you get the same firmware?
  • What is the difference if you use the bit index as template parameter or as a function parameter?
  • These examples only work with a static implementation. Create function templates which support dynamic bit indexes.

Conclusion

C++ templates do not only replace macros, but they also lead to safer and cleaner code.

Do you have any questions, miss information or have feedback? Add a comment below or ask a question on Twitter!

More Posts

Circle Pattern Generator

Posted on 2020-10-01— Common, Software

I developed a small application that generates randomized circle patterns inside a defined rectangle, letting you tweak width, height, precision, spacing and item counts. Results export to SVG and the latest version is available for download. Read the full post for interface details and usage tips.

Read this post

The Importance of Wall Profiles in 3D Printing

Posted on 2023-02-12— 3D Printing, Learn

I'm not a 3D‑printing expert, but I show how simple wall profiles — correct XY/Z thickness, chamfers, and internal recesses — make printed boxes stronger and lighter. I share CAD steps, simulations, and common mistakes in plain terms. Read the full post for practical tips to improve your prints.

Read this post

Logic Gates Puzzle 101

Posted on 2023-02-02— Puzzle

I created a logic-gate puzzle featuring a gated D latch: more artwork than optimized circuitry. I invite you to try decoding the hidden message—there are hints and links to gate and flip-flop basics in the post. I hope you enjoy the challenge; read the full article if you'd like to solve it.

Read this post

Weller WT Solder Station - Great Tool with Great Issues

Posted on 2017-03-20— Common, Review

I bought a Weller WT solder station and appreciate its lightweight pencil, fast heating, and thoughtful features. Still, two persistent problems — a tip retainer that unscrews and one that deforms — reduced usability. I describe the issues, my contact with Weller, and the eventual retainer fix; read the full post for details.

Read this post

Use Enum with More Class!

Posted on 2019-07-29— C++, Improve your Code, Learn

I explain C++11 enum class: why it prevents name conflicts, how it compares to traditional enums and common workarounds, with practical Arduino-friendly examples. If you want clearer, safer enum usage in your interfaces, read the full post for code samples and simple best-practice guidance.

Read this post

Flat Boxes Update

Posted on 2020-11-30— Projects

I've updated the flat boxes set with a refined wall profile (revision B) and added five new boxes, including a large 2×4 and four grid variants. I show corner comparisons and note existing customers received files. If this is useful, please check the full project details on the project page.

Read this post