Bitwise shifting array of char's

后端 未结 7 1584
谎友^
谎友^ 2020-12-03 17:41

I have got an array of chars that I\'m trying to bitwise shift right >>, then & with another array. I think I have got the wrong idea of

7条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-03 18:36

    /**
     * shift a number of bits to the right
     *
     * @param   SRC         the array to shift
     * @param   len         the length of the array
     * @param   shift       the number of consecutive bits to shift
     *
    */
    static void shift_bits_right(uint8_t SRC[], uint16_t len, uint32_t shift) {
        uint32_t i = 0;
    
        uint8_t start = shift / 8;
        uint8_t rest = shift % 8;
        uint8_t previous = 0;
    
        for(i = 0; i < len; i++) {
            if(start <= i) {
                previous = SRC[i - start];
            }
            uint8_t value = (previous << (8 - rest)) | SRC[i + start] >> rest;
            SRC[i + start] = value;
        }
    }
    

提交回复
热议问题