From f34b92ff36da78a963bbfb5ca1c854ab7c464857 Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Sun, 11 Sep 2016 15:26:10 +0200 Subject: [PATCH] Simple bitfield utility --- tstl/include/bit_field.hpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 tstl/include/bit_field.hpp diff --git a/tstl/include/bit_field.hpp b/tstl/include/bit_field.hpp new file mode 100644 index 00000000..af5b9415 --- /dev/null +++ b/tstl/include/bit_field.hpp @@ -0,0 +1,34 @@ +//======================================================================= +// Copyright Baptiste Wicht 2013-2016. +// Distributed under the terms of the MIT License. +// (See accompanying file LICENSE or copy at +// http://www.opensource.org/licenses/MIT) +//======================================================================= + +#ifndef BITFIELD_HPP +#define BITFIELD_HPP + +#include + +namespace std { + +template +struct bit_field { + S* value; + + bit_field(S* value) : value(value) {} + + T operator*() const { + return (*value >> Position) & ((1ULL << Size) - 1); + } + + bit_field& operator=(T new_value){ + S mask = ((1ULL << Size) - 1) << Position; + *value = (*value & ~mask) | ((new_value << Position) & mask); + return *this; + } +}; + +} //end of namespace std + +#endif