@@ -94,26 +94,30 @@ inline void bit_set_zero(byte_t * symbol, unsigned int bit_pos) {
94
94
/**
95
95
* copy bits from most significant bit to least significant
96
96
* e.g. from 5, size 4 -> 5,4,3,2
97
- * @param byte_from: the source
98
- * @param byte_to: the destination ( may use 2 bytes)
97
+ * @param source
98
+ * @param destination: may use 2 bytes
99
99
* @param read_pos: bit position in input
100
100
* @param write_pos: bit position in output
101
101
* @param size: number of bits to copy
102
102
*/
103
- void bit_copy (byte_t byte_from , byte_t * byte_to , unsigned int read_pos , unsigned int write_pos , int size ) {
103
+ void bit_copy (byte_t source , byte_t * destination , int read_pos , int write_pos , int size ) {
104
+ //TODO: need to improve, actually it's copying bit x bit...
104
105
for (int offset = 0 ; offset < size ; offset ++ ) {
105
- unsigned int from = read_pos - offset ;
106
- int to = write_pos - offset ;
107
- if (to < 0 ) {
108
- to += SYMBOL_BITS ;
106
+ int source_idx = read_pos - offset ;
107
+ int dest_idx = write_pos - offset ;
108
+ if (dest_idx < 0 ) {
109
+ // we have already moved to the next dest byte
110
+ // add 8 bits to the dest_idx since it's negative now
111
+ dest_idx += SYMBOL_BITS ;
109
112
}
110
113
111
- byte_t bit = (byte_t ) ( byte_from >> from ) & SINGLE_BIT_1 ; /* Get the source bit as 0/1 symbol */
112
- * byte_to &= ~(( byte_t )( SINGLE_BIT_1 << to )) ; /* clear destination bit */
113
- * byte_to |= (byte_t ) ( bit << to ); /* set destination bit */
114
+ byte_t bit_val = (source >> source_idx ) & SINGLE_BIT_1 ; /* get bit_val from source. it will be ..00 or ..01 */
115
+ * destination &= ~(SINGLE_BIT_1 << dest_idx ) ; /* set to 0 the destination bit */
116
+ * destination |= (bit_val << dest_idx ); /* set to bit_val the destination bit */
114
117
115
- if (to == 0 ) {
116
- byte_to ++ ;
118
+ if (dest_idx == 0 ) {
119
+ // we have used all bits from destination, move to the next byte
120
+ destination ++ ;
117
121
}
118
122
}
119
123
}
0 commit comments