opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
liboscar
utilities
bitwise_helpers.h
Go to the documentation of this file.
1
#pragma once
2
3
#include <concepts>
4
5
namespace
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
_
int
egral 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
}
osc
Definition
custom_decoration_generator.h:5
osc::swap_single_bit
constexpr T swap_single_bit(T value, int bit_index0, int bit_index1)
Definition
bitwise_helpers.h:10
osc::Key::T
@ T
Generated by
1.9.8