How to print bits in c

前端 未结 5 1142
礼貌的吻别
礼貌的吻别 2021-01-21 10:30

I\'m writing a function to print bits in c, I\'m only allowed to use write function. my function doesn\'t work for other numbers.

void    print_bit         


        
5条回答
  •  爱一瞬间的悲伤
    2021-01-21 11:01

    Found somewhere on Internet:

    void printBits( void const * const ptr, size_t const size )
    {   printf( "hexVal = %#08x; decVal = %i \n", * ( uint* )ptr, * ( uint* )ptr );
        unsigned char *b = ( unsigned char* ) ptr;
        unsigned char byte;
        int i, j;
        for (i=size-1;i>=0;i--)
           for (j=7;j>=0;j--)
            {   byte = (b[i] >> j) & 1;
                printf( " %u ", byte );
            }
        puts("");
        for( int ind  = 31; ind > 9; ind-- )
            printf( "%d ", ind );
        for( int ind  = 9; ind > -1; ind-- )
            printf( " %d ", ind );      
        puts(""); puts("");
    };
    
    void compareValsBits( const void * ptrA, size_t sizeA, const void * ptrB, size_t sizeB )
    {   if ( sizeA != sizeB ) return;
        printf( "comparing: \n[%#08x](%i)\n[%#08x](%i)\n====\n", * ( uint* )ptrA, * ( uint* )ptrA, * ( uint* )ptrB, * ( uint* )ptrB );
        unsigned char *a = ( unsigned char* ) ptrA;
        unsigned char *b = ( unsigned char* ) ptrB;
        unsigned char byteA, byteB;
        int i, j;
    
        for (i=sizeA-1;i>=0;i--)
            for (j=7;j>=0;j--)
            {   byteA = (a[i] >> j) & 1;
                printf( " %u ", byteA );
            }
        puts("");
        for (i=sizeA-1;i>=0;i--)
            for (j=7;j>=0;j--)
            {   byteB = (b[i] >> j) & 1;
                printf( " %u ", byteB );
            }
        puts("");
        for( int ind  = 31; ind > 9; ind-- )
            printf( "%d ", ind );
        for( int ind  = 9; ind > -1; ind-- )
            printf( " %d ", ind );      
        puts(""); 
    
        for (i=sizeA-1;i>=0;i--)
            for (j=7;j>=0;j--)
            {   byteA = (a[i] >> j) & 1;
                byteB = (b[i] >> j) & 1;
                if ( byteA == byteB )
                    printf( "   " );
                else
                    printf( " x " );
            }   
        printf( "\n====\n" );
    };
    

    Some trivial macro unrolling for setting, and resetting bits: https://stackoverflow.com/questions/1872220/is-it-possible-to-iterate-over-arguments-in-variadic-macros

    #include 
    typedef __uint32_t uint;
    //==============================================================================================
    #define STRINGIZE(arg)                  #arg
    #define CONCATENATE(arg1, arg2)             arg1##arg2
    #define FOR_EACH_1(macroName, param, x, ...)        macroName(param, x)
    #define FOR_EACH_2(macroName, param, x, ...)        macroName(param, x);    FOR_EACH_1(macroName, param,  __VA_ARGS__);
    #define FOR_EACH_3(macroName, param, x, ...)        macroName(param, x);    FOR_EACH_2(macroName, param,  __VA_ARGS__);
    #define FOR_EACH_4(macroName, param, x, ...)        macroName(param, x);    FOR_EACH_3(macroName, param,  __VA_ARGS__);
    #define FOR_EACH_5(macroName, param, x, ...)        macroName(param, x);    FOR_EACH_4(macroName, param,  __VA_ARGS__);
    #define FOR_EACH_6(macroName, param, x, ...)        macroName(param, x);    FOR_EACH_5(macroName, param,  __VA_ARGS__);
    #define FOR_EACH_7(macroName, param, x, ...)        macroName(param, x);    FOR_EACH_6(macroName, param,  __VA_ARGS__);
    #define FOR_EACH_8(macroName, param, x, ...)        macroName(param, x);    FOR_EACH_7(macroName, param,  __VA_ARGS__);
    #define FOR_EACH_NARG(...)              FOR_EACH_NARG_(__VA_ARGS__, FOR_EACH_RSEQ_N())
    #define FOR_EACH_NARG_(...)                 FOR_EACH_ARG_N(__VA_ARGS__) 
    #define FOR_EACH_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, N, ...) N 
    #define FOR_EACH_RSEQ_N() 8, 7, 6, 5, 4, 3, 2, 1, 0
    #define FOR_EACH_(N, macroName, param, x, ...)      CONCATENATE(FOR_EACH_, N)(macroName, param, x, __VA_ARGS__)
    #define FOR_EACH(macroName, param, x, ...)      FOR_EACH_(FOR_EACH_NARG(x, __VA_ARGS__), macroName, param, x, __VA_ARGS__)
    //============================================================================================
    //--------------------------------------------------------------------------------------------
    //------------------------  MACRO KERNELS ----------------------------------------------------
    //--------------------------------------------------------------------------------------------
    #define SET_BIT( VARIABLE, bitNo )      { VARIABLE = ( ( ( VARIABLE >> bitNo ) & 0x1u ) != 0x1u )   \
                        ? VARIABLE |= 0x1u << bitNo                     \
                        : VARIABLE = VARIABLE;                      \
                        }
    #define RESET_BIT( VARIABLE, bitNo )    { VARIABLE = ( ( ( VARIABLE >> bitNo ) & 0x1u ) != 0x0u )   \
                        ? VARIABLE &= ~( 0x1u << bitNo )                \
                        : VARIABLE = VARIABLE;                      \
                        }
    #define BIT_VAL( VARIABLE, bitNo ) ( VARIABLE >> bitNo ) & 0x1u
    //--------------------------------------------------------------------------------------------
    //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    uint a = 0x0u;
    int main( void )
    {   FOR_EACH( SET_BIT, a, 0, 1, 2 );
        printf( "0x%.8X\n", a );
        FOR_EACH( RESET_BIT, a, 1, 2 );
        printf( "0x%.8X\n", a );
        return 0;
    };//end of main()
    //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    

提交回复
热议问题