#include<stdio.h>
#include<iostream>
#ifndef _BIT_H
#define _BIT_H

typedef char INT1;
typedef unsigned char UINT1;
typedef short INT2;
typedef unsigned short UINT2;
typedef int INT4;
typedef unsigned int UINT4;

#define BITS_PER_BYTE 8
#define BYTE_BIT8     0x80
UINT1  gau1BitMaskMap[8] = { 0x01, 0x80, 0x40, 0x20, 0x10,0x08, 0x04, 0x02 };


/*关于一些位运算的应用
 &位逻辑与
 |位逻辑或
 ^位逻辑异或
 ~位逻辑反
 >>
 <<  */


/*au1List1数组里的元素与au1List2数组里的元素进行按位或运算(进行按位或运算的元素个数为u4TmpSize)*/
/* |=  有一为一*/
#define ADD_PORT_LIST(au1List1, au1List2, u4Size1, u4Size2) \
              {\
                 UINT4 u4TmpSize; \
                 UINT2 u2ByteIndex; \
                 \
                 (u4Size1 < u4Size2) ? \
                            (u4TmpSize = u4Size1) : (u4TmpSize = u4Size2); \
                 \
                 for (u2ByteIndex = 0;\
                      u2ByteIndex < u4TmpSize;\
                      u2ByteIndex++) \
                 {\
                    au1List1[u2ByteIndex] |= au1List2[u2ByteIndex];\
                 }\
              }

/*au1List1数组里的元素与au1List2数组里的元素进行按位与运算(进行按位与运算的元素个数为u4TmpSize)*/
/* &=  两个为一才为一*/
#define AND_PORT_LIST(au1List1, au1List2, u4Size1, u4Size2) \
              {\
                 UINT4 u4TmpSize; \
                 UINT2 u2ByteIndex; \
                 \
                 (u4Size1 < u4Size2) ? \
                            (u4TmpSize = u4Size1) : (u4TmpSize = u4Size2); \
                 \
                 for (u2ByteIndex = 0;\
                      u2ByteIndex < u4TmpSize;\
                      u2ByteIndex++) \
                 {\
                    au1List1[u2ByteIndex] &= au1List2[u2ByteIndex];\
                 }\
              }


/*au1List1数组里的元素与au1List2数组里的元素进行按位异或运算(进行按位异或运算的元素个数为u4TmpSize)*/
/*^= 相同为0,不同为一*/
#define XOR_BITLIST(au1List1, au1List2, u4Size1, u4Size2) \
              {\
                 UINT4 u4TmpSize; \
                 UINT2 u2ByteIndex; \
                 \
                 (u4Size1 < u4Size2) ? \
                            (u4TmpSize = u4Size1) : (u4TmpSize = u4Size2); \
                 \
                 for (u2ByteIndex = 0;\
                      u2ByteIndex < u4TmpSize;\
                      u2ByteIndex++) \
                 {\
                    au1List1[u2ByteIndex] ^= au1List2[u2ByteIndex];\
                 }\
              }
/*将数组au1List里的每一个元素进行取反操作 ~ */
#define NOT_PORT_LIST(au1List, u4ListSize) \
              {\
                 UINT2 u2ByteIndex;\
                 \
                 for (u2ByteIndex = 0;\
                      u2ByteIndex < u4ListSize;\
                      u2ByteIndex++) {\
                    au1List[u2ByteIndex] = ~au1List[u2ByteIndex];\
                 }\
              }

/* a &= ~b (a = a & (~b) */
#define RESET_PORT_LIST(au1List1, au1List2, u4Size1, u4Size2) \
              {\
                 UINT4 u4TmpSize; \
                 UINT2 u2ByteIndex;\
                 \
                 (u4Size1 < u4Size2) ? \
                            (u4TmpSize = u4Size1) : (u4TmpSize = u4Size2); \
                 for (u2ByteIndex = 0;\
                      u2ByteIndex < u4TmpSize;\
                      u2ByteIndex++) {\
                    au1List1[u2ByteIndex] &= ~au1List2[u2ByteIndex];\
                 }\
              }


/*判断数组里的每一个比特位是否为一,一个数组元素可以代表8个比特位,是则返回TRUE*/
#define BITLIST_IS_BIT_SET(au1BitArray, u2BitNumber, i4ArraySize, bResult) \
        {\
           UINT2 u2BitNumberBytePos;\
           UINT2 u2BitNumberBitPos;\
           u2BitNumberBytePos = (UINT2)(u2BitNumber / BITS_PER_BYTE);\
           u2BitNumberBitPos  = (UINT2)(u2BitNumber % BITS_PER_BYTE);\
           bResult = OSIX_FALSE;      \
           if (u2BitNumberBitPos  == 0) {u2BitNumberBytePos -= 1;} \
           \
              if (u2BitNumberBytePos < i4ArraySize) \
              {                                     \
                  if ((au1BitArray[u2BitNumberBytePos] \
                       & gau1BitMaskMap[u2BitNumberBitPos]) != 0) {\
                      \
                          bResult = OSIX_TRUE;\
                  }\
              } \
        }


/*将数字转换成比特位,例如数字6,就将数组下标为0的元素的第6个比特位置为1*/
#define BITLIST_SET_BIT(au1BitArray, u2BitNumber, i4ArraySize) \
           {\
              UINT2 u2BitNumberBytePos;\
              UINT2 u2BitNumberBitPos;\
              u2BitNumberBytePos = (UINT2)(u2BitNumber / BITS_PER_BYTE);\
              u2BitNumberBitPos  = (UINT2)(u2BitNumber % BITS_PER_BYTE);\
            if (u2BitNumberBitPos  == 0) {u2BitNumberBytePos -= 1;} \
              \
              if (u2BitNumberBytePos < i4ArraySize) \
              {                                     \
                au1BitArray[u2BitNumberBytePos] = (UINT1)(au1BitArray[u2BitNumberBytePos] \
                               | gau1BitMaskMap[u2BitNumberBitPos]);\
              }                                     \
           }


/*将某个元素的某一个比特位置为0*/
#define BITLIST_RESET_BIT(au1BitArray, u2BitNumber, i4ArraySize) \
              {\
                 UINT2 u2BitNumberBytePos;\
                 UINT2 u2BitNumberBitPos;\
                 u2BitNumberBytePos = (UINT2)(u2BitNumber / BITS_PER_BYTE);\
                 u2BitNumberBitPos  = (UINT2)(u2BitNumber % BITS_PER_BYTE);\
             if (u2BitNumberBitPos  == 0) {u2BitNumberBytePos -= 1;} \
                 \
                 if (u2BitNumberBytePos < i4ArraySize) \
                 {                                     \
                     au1BitArray[u2BitNumberBytePos] = (UINT1)(au1BitArray[u2BitNumberBytePos] \
                               & ~gau1BitMaskMap[u2BitNumberBitPos]);\
                 }                                     \
              }


#endif
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐