C/C++ efficient bit array

前端 未结 10 841
清酒与你
清酒与你 2020-11-29 02:15

Can you recommend efficient/clean way to manipulate arbitrary length bit array?

Right now I am using regular int/char bitmask, but those are not very clean when arra

10条回答
  •  爱一瞬间的悲伤
    2020-11-29 02:46

    This posting is rather old, but there is an efficient bit array suite in C in my ALFLB library.

    For many microcontrollers without a hardware-division opcode, this library is EFFICIENT because it doesn't use division: instead, masking and bit-shifting are used. (Yes, I know some compilers will convert division by 8 to a shift, but this varies from compiler to compiler.)

    It has been tested on arrays up to 2^32-2 bits (about 4 billion bits stored in 536 MBytes), although last 2 bits should be accessible if not used in a for-loop in your application.

    See below for an extract from the doco. Doco is http://alfredo4570.net/src/alflb_doco/alflb.pdf, library is http://alfredo4570.net/src/alflb.zip

    Enjoy,
    Alf

    //------------------------------------------------------------------
    BM_DECLARE( arrayName, bitmax);
            Macro to instantiate an array to hold bitmax bits.
    //------------------------------------------------------------------
    UCHAR *BM_ALLOC( BM_SIZE_T bitmax); 
            mallocs an array (of unsigned char) to hold bitmax bits.
            Returns: NULL if memory could not be allocated.
    //------------------------------------------------------------------
    void BM_SET( UCHAR *bit_array, BM_SIZE_T bit_index);
            Sets a bit to 1.
    //------------------------------------------------------------------
    void BM_CLR( UCHAR *bit_array, BM_SIZE_T bit_index);
            Clears a bit to 0.
    //------------------------------------------------------------------
    int BM_TEST( UCHAR *bit_array, BM_SIZE_T bit_index); 
            Returns: TRUE (1) or FALSE (0) depending on a bit.
    //------------------------------------------------------------------
    int BM_ANY( UCHAR *bit_array, int value, BM_SIZE_T bitmax); 
            Returns: TRUE (1) if array contains the requested value (i.e. 0 or 1).
    //------------------------------------------------------------------
    UCHAR *BM_ALL( UCHAR *bit_array, int value, BM_SIZE_T bitmax);
            Sets or clears all elements of a bit array to your value. Typically used after a BM_ALLOC.  
            Returns: Copy of address of bit array
    //------------------------------------------------------------------
    void BM_ASSIGN( UCHAR *bit_array, int value, BM_SIZE_T bit_index);
            Sets or clears one element of your bit array to your value.
    //------------------------------------------------------------------
    BM_MAX_BYTES( int bit_max); 
            Utility macro to calculate the number of bytes to store bitmax bits.
            Returns: A number specifying the number of bytes required to hold bitmax bits.
    //------------------------------------------------------------------
    

提交回复
热议问题