mirror of
https://github.com/espressif/openthread.git
synced 2026-08-31 06:19:54 +00:00
[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:
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user