units
Use physical dimensions at compile-time or run-time.
bit-range.hpp
Go to the documentation of this file.
1 /// @file vnix/bit-range.hpp
2 /// @brief Definition of vnix::bit, vnix::bit_range.
3 /// @copyright 2019 Thomas E. Vaughan; all rights reserved.
4 /// @license BSD Three-Clause; see LICENSE.
5 
6 #ifndef VNIX_BIT_HPP
7 #define VNIX_BIT_HPP
8 
9 namespace vnix {
10 
11 
12 /// Word with specified bit set.
13 /// @tparam I Type of integer word.
14 /// @param n Offset of bit in word.
15 template <typename I> constexpr I bit(unsigned n) { return I(1) << n; }
16 
17 
18 /// Word with specified range of bits set.
19 /// @tparam I Type of integer word.
20 /// @param n1 Offset of bit at one end of range.
21 /// @param n2 Offset of bit at other end of range.
22 template <typename I> constexpr I bit_range(unsigned n1, unsigned n2) {
23  if (n1 < n2) { return bit<I>(n1) | bit_range<I>(n1 + 1, n2); }
24  if (n2 < n1) { return bit<I>(n2) | bit_range<I>(n2 + 1, n1); }
25  return bit<I>(n1);
26 }
27 
28 
29 } // namespace vnix
30 
31 #endif // ndef VNIX_BIT_HPP
Thomas E. Vaughan&#39;s public software.
Definition: rational.hpp:13
constexpr I bit_range(unsigned n1, unsigned n2)
Word with specified range of bits set.
Definition: bit-range.hpp:22
constexpr I bit(unsigned n)
Word with specified bit set.
Definition: bit-range.hpp:15