From 5b1067f8d1e6a8452c1b4028422b2c3d989a5341 Mon Sep 17 00:00:00 2001 From: coffee Date: Fri, 29 May 2026 14:44:03 +0800 Subject: [PATCH] =?UTF-8?q?1.=20HBit=E5=A2=9E=E5=8A=A0=E5=8F=AF=E5=AF=B9?= =?UTF-8?q?=E6=AF=94=E5=92=8C=E8=B5=8B=E5=80=BC=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/HBit.h | 8 ++++++++ src/HBit.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/include/HBit.h b/include/HBit.h index 4621dd7..038bc37 100644 --- a/include/HBit.h +++ b/include/HBit.h @@ -83,5 +83,13 @@ HBitType HBitAll(const HBitType *data); // 如果所有比特为0, 返回1, 否则返回0 HBitType HBitNone(const HBitType *data); +// 获取比特长度 +HBitLenType HBitGetLen(const HBitType *data); + +// 复制比特, 需要两者长度一致, 否则返回0 +uint8_t HBitMemcpy(const HBitType *dst, HBitType *src); + +// 比较两个比特是否相等, 需要两者长度一致, 相同返回1, 否则返回0 +uint8_t HBitEqual(const HBitType *a, const HBitType *b); #endif //__H_BIT__ diff --git a/src/HBit.c b/src/HBit.c index 8b1463d..633cdfc 100644 --- a/src/HBit.c +++ b/src/HBit.c @@ -1,6 +1,7 @@ #include +#include #ifndef LogD #define LogD(...) @@ -113,3 +114,36 @@ HBitType HBitAll(const HBitType *data) { HBitType HBitNone(const HBitType *data) { return !HBitAny(data); } + +HBitLenType HBitGetLen(const HBitType *data) { + return GetBitLen(data); +} + +uint8_t HBitMemcpy(const HBitType *dst, HBitType *src) { + if (!src || !dst) { + return 0; + } + + if (GetBitLen(src) != GetBitLen(dst)) { + return 0; + } + + _HBitBase *srcBase = (_HBitBase *)src; + _HBitBase *dstBase = (_HBitBase *)dst; + memcpy(dstBase->data, srcBase->data, _HBIT_BYTE_INDEX(srcBase->len) + 1); + return 1; +} + +uint8_t HBitEqual(const HBitType *a, const HBitType *b) { + if (!a || !b) { + return 0; + } + + if (GetBitLen(a) != GetBitLen(b)) { + return 0; + } + + _HBitBase *aBase = (_HBitBase *)a; + _HBitBase *bBase = (_HBitBase *)b; + return memcmp(aBase->data, bBase->data, _HBIT_BYTE_INDEX(aBase->len) + 1) == 0; +}