Skip to content

Commit eeaab1b

Browse files
committed
small refactoring, adding comments
1 parent 7bec698 commit eeaab1b

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

bin_io.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,26 +94,30 @@ inline void bit_set_zero(byte_t * symbol, unsigned int bit_pos) {
9494
/**
9595
* copy bits from most significant bit to least significant
9696
* 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
9999
* @param read_pos: bit position in input
100100
* @param write_pos: bit position in output
101101
* @param size: number of bits to copy
102102
*/
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...
104105
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;
109112
}
110113

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 */
114117

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++;
117121
}
118122
}
119123
}

bin_io.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ FILE* bin_open_update(const char *filename);
5050
byte_t bit_check(byte_t symbol, unsigned int bit_pos);
5151
void bit_set_one(byte_t * symbol, unsigned int bit_pos);
5252
void bit_set_zero(byte_t * symbol, unsigned int bit_pos);
53-
void bit_copy(byte_t byte_from, byte_t *byte_to, unsigned int read_pos, unsigned int write_pos, int size);
53+
void bit_copy(byte_t source, byte_t *destination, int read_pos, int write_pos, int size);
5454

5555
int get_available_bits(long buffer_bit_idx);
5656
int bit_pos_in_current_byte(long buffer_idx);

0 commit comments

Comments
 (0)