Zserio C++ runtime library  1.0.1
Built for Zserio 2.14.0
BitFieldUtil.cpp
Go to the documentation of this file.
1 #include "zserio/BitFieldUtil.h"
3 
4 namespace zserio
5 {
6 
7 static void checkBitFieldLength(size_t length)
8 {
9  if (length == 0 || length > 64)
10  throw CppRuntimeException("Asking for bound of bitfield with invalid length ") << length << "!";
11 }
12 
13 int64_t getBitFieldLowerBound(size_t length, bool isSigned)
14 {
15  checkBitFieldLength(length);
16 
17  if (isSigned)
18  return -static_cast<int64_t>((UINT64_C(1) << (length - 1)) - 1) - 1;
19  else
20  return 0;
21 }
22 
23 uint64_t getBitFieldUpperBound(size_t length, bool isSigned)
24 {
25  checkBitFieldLength(length);
26 
27  if (isSigned)
28  return (UINT64_C(1) << (length - 1)) - 1;
29  else
30  return length == 64 ? UINT64_MAX : ((UINT64_C(1) << length) - 1);
31 }
32 
33 } // namespace zserio
int64_t getBitFieldLowerBound(size_t length, bool isSigned)
uint64_t getBitFieldUpperBound(size_t length, bool isSigned)