From e63e2899acd5aef5c9d3e2785e732a7ee7466cc7 Mon Sep 17 00:00:00 2001 From: coffee Date: Sat, 28 Mar 2026 08:42:10 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E4=BC=98=E5=8C=96Bit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/HBit.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/HBit.c b/src/HBit.c index 20bb9ae..8b1463d 100644 --- a/src/HBit.c +++ b/src/HBit.c @@ -6,6 +6,10 @@ #define LogD(...) #endif +#define _HBIT_BYTE_INDEX(index) (index >> 3) +#define _HBIT_BIT_OFFSET(index) (index & 0x7) +#define _HBIT_ALIGNED_LEN(len) (len & (~(HBitLenType)0x7)) + static void InitHBit(HBitType *data) { if ((data[0] & kHBitInitFlag) == 0) { return; @@ -25,12 +29,12 @@ static HBitLenType GetBitLen(const HBitType *data) { static void SetBit(HBitType *data, HBitIndexType index, HBitType value) { _HBitBase *base = (_HBitBase *)data; - base->data[index / 8] = (base->data[index / 8] & ~(0x01 << (index % 8))) | ((!!value) << (index % 8)); + base->data[_HBIT_BYTE_INDEX(index)] = (base->data[_HBIT_BYTE_INDEX(index)] & ~(0x01 << _HBIT_BIT_OFFSET(index))) | ((!!value) << _HBIT_BIT_OFFSET(index)); } static HBitType GetBit(const HBitType *data, HBitIndexType index) { _HBitBase *base = (_HBitBase *)data; - return (base->data[index / 8] >> (index % 8)) & 0x01; + return (base->data[_HBIT_BYTE_INDEX(index)] >> _HBIT_BIT_OFFSET(index)) & 0x01; } void HBitSet(HBitType *data, HBitIndexType index, HBitType value) { @@ -56,11 +60,11 @@ HBitType HBitGet(const HBitType *data, HBitIndexType index) { void HBitFill(HBitType *data, HBitType value) { InitHBit(data); _HBitBase *base = (_HBitBase *)data; - for (int i = 0; i < base->len / 8; ++i) { + for (int i = 0; i < _HBIT_BYTE_INDEX(base->len); ++i) { base->data[i] = value ? 0xFF : 0x00; } - for (int i = base->len / 8 * 8; i < base->len; ++i) { + for (int i = _HBIT_ALIGNED_LEN(base->len); i < base->len; ++i) { SetBit(data, i, value); } } @@ -73,13 +77,13 @@ void HBitReverse(HBitType *data, HBitIndexType index) { HBitType HBitAny(const HBitType *data) { const _HBitBase *base = (_HBitBase *)data; InitHBit((HBitType *)data); - for (int i = 0; i < base->len / 8; ++i) { + for (HBitLenType i = 0; i < _HBIT_BYTE_INDEX(base->len); ++i) { if (base->data[i] != 0x00) { return 1; } } - for (int i = base->len / 8 * 8; i < base->len; ++i) { + for (HBitLenType i = _HBIT_ALIGNED_LEN(base->len); i < base->len; ++i) { if (HBitGet(data, i)) { return 1; } @@ -91,13 +95,13 @@ HBitType HBitAny(const HBitType *data) { HBitType HBitAll(const HBitType *data) { const _HBitBase *base = (_HBitBase *)data; InitHBit((HBitType *)data); - for (int i = 0; i < base->len / 8; ++i) { + for (HBitLenType i = 0; i < _HBIT_BYTE_INDEX(base->len); ++i) { if (base->data[i] != 0xFF) { return 0; } } - for (int i = base->len / 8 * 8; i < base->len; ++i) { + for (HBitLenType i = _HBIT_ALIGNED_LEN(base->len); i < base->len; ++i) { if (!HBitGet(data, i)) { return 0; }