C language binary flip by bit

c language binary flip(reverse) by bit .


/* char reverse */
static char char_bit_reverse(char input_val)
{
    char i = 0;
    char output_val = 0;
    
    for (i = 1; i < 8; i++) {
        output_val += input_val & 1;
        output_val <<= 1;
        input_val >>= 1;
    }
    
    return output_val;
}

C
/* short reverse */
static short short_bit_reverse(short input_val)
{
    char i = 0;
    short output_val = 0;
    
    for (i = 1; i < 16; i++) {
        output_val += input_val & 1;
        output_val <<= 1;
        input_val >>= 1;
    }
    
    return output_val;
}

C
/* int reverse */
static int int_bit_reverse(int input_val)
{
    char i = 0;
    int output_val = 0;
    
    for (i = 1; i < 32; i++) {
        output_val += input_val & 1;
        output_val <<= 1;
        input_val >>= 1;
    }
    
    return output_val;
}
C++
/* int64 reverse */
static int64 int64_bit_reverse(int64 input_val)
{
    char i = 0;
    int64 output_val = 0;
    
    for (i = 1; i < 64; i++) {
        output_val += input_val & 1;
        output_val <<= 1;
        input_val >>= 1;
    }
    
    return output_val;
}

 

Comments

Popular posts from this blog

Python Receiving and parse JSON Data via UDP protocol

ubus lua client method and event registration code demo/example