opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
bitwise_helpers.h
Go to the documentation of this file.
1#pragma once
2
3#include <concepts>
4
5namespace osc
6{
7 // Returns `value`, but with the value of the bit at `bit_index0` swapped
8 // with the value of the bit at `bit_index1`.
9 template<std::unsigned_integral T>
10 constexpr T swap_single_bit(T value, int bit_index0, int bit_index1)
11 {
12 // Similar approach to the XOR swapping algorithm for integers:
13 //
14 // https://en.wikipedia.org/wiki/XOR_swap_algorithm
15
16 const T bit0 = (value >> bit_index0) & T{1u};
17 const T bit1 = (value >> bit_index1) & T{1u};
18
19 T x = bit0 ^ bit1;
20 x = (x << bit_index0) | (x << bit_index1);
21 return x ^ value;
22 }
23}
Definition custom_decoration_generator.h:5
constexpr T swap_single_bit(T value, int bit_index0, int bit_index1)
Definition bitwise_helpers.h:10
constexpr U to(T &&value)
Definition conversion.h:56