bitmanip.h
Go to the documentation of this file.
1 //
2 // Bareflank Hypervisor
3 //
4 // Copyright (C) 2015 Assured Information Security, Inc.
5 // Author: Rian Quinn <quinnr@ainfosec.com>
6 // Author: Brendan Kerrigan <kerriganb@ainfosec.com>
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 
22 #ifndef BITMANIP_H
23 #define BITMANIP_H
24 
25 #include <type_traits>
26 
27 template<class T, class B,
28  class = typename std::enable_if<std::is_integral<T>::value>::type,
29  class = typename std::enable_if<std::is_integral<B>::value>::type>
30 auto set_bit(T t, B b) noexcept
31 { return t | (0x1UL << b); }
32 
33 template<class T, class B,
34  class = typename std::enable_if<std::is_integral<T>::value>::type,
35  class = typename std::enable_if<std::is_integral<B>::value>::type>
36 auto clear_bit(T t, B b) noexcept
37 { return t & ~(0x1UL << b); }
38 
39 template<class T, class B,
40  class = typename std::enable_if<std::is_integral<T>::value>::type,
41  class = typename std::enable_if<std::is_integral<B>::value>::type>
42 auto get_bit(T t, B b) noexcept
43 { return (t & (0x1UL << b)) >> b; }
44 
45 template<class T, class B,
46  class = typename std::enable_if<std::is_integral<T>::value>::type,
47  class = typename std::enable_if<std::is_integral<B>::value>::type>
48 auto is_bit_set(T t, B b) noexcept
49 { return get_bit(t, b) != 0; }
50 
51 template<class T, class B,
52  class = typename std::enable_if<std::is_integral<T>::value>::type,
53  class = typename std::enable_if<std::is_integral<B>::value>::type>
54 auto is_bit_cleared(T t, B b) noexcept
55 { return get_bit(t, b) == 0; }
56 
57 template<class T,
58  class = typename std::enable_if<std::is_integral<T>::value>::type>
60 { return __builtin_popcountll(t); }
61 
62 template<class T, class M,
63  class = typename std::enable_if<std::is_integral<T>::value>::type,
64  class = typename std::enable_if<std::is_integral<M>::value>::type>
65 auto get_bits(T t, M m) noexcept
66 { return t & m; }
67 
68 template<class T, class M, class V,
69  class = typename std::enable_if<std::is_integral<T>::value>::type,
70  class = typename std::enable_if<std::is_integral<M>::value>::type,
71  class = typename std::enable_if<std::is_integral<V>::value>::type>
72 auto set_bits(T t, M m, V v) noexcept
73 { return (t & ~m) | (v & m); }
74 
75 #endif
auto num_bits_set(T t) noexcept
Definition: bitmanip.h:59
auto get_bit(T t, B b) noexcept
Definition: bitmanip.h:42
void uint64_t uint64_t uint64_t *rdx noexcept
auto is_bit_cleared(T t, B b) noexcept
Definition: bitmanip.h:54
auto clear_bit(T t, B b) noexcept
Definition: bitmanip.h:36
auto set_bit(T t, B b) noexcept
Definition: bitmanip.h:30
auto get_bits(T t, M m) noexcept
Definition: bitmanip.h:65
auto is_bit_set(T t, B b) noexcept
Definition: bitmanip.h:48
auto set_bits(T t, M m, V v) noexcept
Definition: bitmanip.h:72