[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
+1 -1
View File
@@ -210,7 +210,7 @@ void Notifier::LogEvents(Events aEvents) const
bool didLog = false;
String<kFlagsStringBufferSize> string;
for (uint8_t bit = 0; bit < sizeof(Events::Flags) * CHAR_BIT; bit++)
for (uint8_t bit = 0; bit < BitSizeOf(Events::Flags); bit++)
{
VerifyOrExit(flags != 0);