[num-limits] add kBitsPerByte, BitSizeOf(), and BytesForBitSize() (#9618)

This commit removes the use of `CHAR_BIT` in the code and instead
defines a constant `kBitsPerByte` for this in `numeric_limits.hpp`.
`CHAR_BIT` is generally defined as `8` but there are some platforms
were this may not be the case. The way we use `CHAR_BIT` is to
determine number of bits in a byte, so introducing our own constant
`kBitsPerByte` helps improve code readability and safety.

This commit also adds `BitSizeOf()` macro which is similar to `sizeof()`
but returns the number of bits of a given type or variable. Macro
`BytesForBitSize()` is also added which returns the number of bytes
needed to represent a given bit size. This replaces the
`BitVectorBytes()` macro previously defined in `encoding.hpp`.
This commit is contained in:
Abtin Keshavarzian
2023-11-19 20:10:40 -08:00
committed by GitHub
parent 7936f8bcf9
commit ce9edaf8b5
32 changed files with 104 additions and 77 deletions
+2 -1
View File
@@ -28,6 +28,7 @@
#include "common/encoding.hpp"
#include "common/message.hpp"
#include "common/numeric_limits.hpp"
#include "common/random.hpp"
#include "instance/instance.hpp"
#include "net/checksum.hpp"
@@ -160,7 +161,7 @@ void CorruptMessage(Message &aMessage)
SuccessOrQuit(aMessage.Read(byteOffset, byte));
bitOffset = Random::NonCrypto::GetUint8InRange(0, CHAR_BIT);
bitOffset = Random::NonCrypto::GetUint8InRange(0, kBitsPerByte);
byte ^= (1 << bitOffset);