B
ben
i have a bit of code, that works absolutely fine as is, but seems over
complicated/long winded. is there anyway to shorten/simplify it?
the code is below. description of it: it's like strcpy in that it
copies one block of data to another block of data until the block that
is being copied contains a zero/null. the difference with this code is
that it's doing 4bits at a time (all the values are 4bits) and the two
blocks of data may not be aligned the same - that is the start of the
data that's being copied could be the rightmost 4bits of a normal byte,
and the start of the data that's being written to could be the leftmost
4bits of a byte for example - so out of sync with each other as it
were. also the terminating NULL is a 4bit value.
// 'datatocopy' is a u_int8_t pointer to the start of the data to copy.
// 'tocopyto' is a u_int8_t pointer to the start of the space to
// copy to.
// 'st' is an int for adding to datatocopy to step along it (the number
// of 4bit blocks from the left (starting at 0)).
// 'st2' dito, but for tocopyto.
while( ((*(datatocopy + ((st % 2 ? st-1 : st)/2 ))) >> (st % 2 ? 0 :
4) & 0xf) != 0 ) { // steps along and reads 4bits at a time
*(tocopyto + ((st2 % 2 ? st2 - 1 : st2) / 2) ) |= 0xf << (st2
% 2 ? 0 : 4); // zero the 4 bits...
*(tocopyto + ((st2 % 2 ? st2 - 1 : st2) / 2) ) ^= 0xf << (st2
% 2 ? 0 : 4); // ...about to be written to
tmp = *(datatocopy + ((st % 2 ? st-1 : st)/2 )); // get the
8bit byte that includes required 4bits
if( st % 2 ) { // it's odd (required 4bits are on the right).
tmp |= 0xf0; // delete the,
tmp ^= 0xf0; // left 4bits.
} else // it's even (required 4bits on the left).
tmp >>= 4; // shift to the right.
*(tocopyto + ((st2 % 2 ? st2 - 1 : st2) / 2)) |= tmp << ( st2
% 2 ? 0 : 4); // copy the 4bits
st++, st2++; // increment 4bit block counters
}
i'm using an intermediate temperory variable half way through - is that
necessary? it just seems an awful lot to do what it's doing.
i'd just like to really consolidate it somehow - possible? easy?
pointers to how to?
any help much apprecaited.
thanks, ben.
complicated/long winded. is there anyway to shorten/simplify it?
the code is below. description of it: it's like strcpy in that it
copies one block of data to another block of data until the block that
is being copied contains a zero/null. the difference with this code is
that it's doing 4bits at a time (all the values are 4bits) and the two
blocks of data may not be aligned the same - that is the start of the
data that's being copied could be the rightmost 4bits of a normal byte,
and the start of the data that's being written to could be the leftmost
4bits of a byte for example - so out of sync with each other as it
were. also the terminating NULL is a 4bit value.
// 'datatocopy' is a u_int8_t pointer to the start of the data to copy.
// 'tocopyto' is a u_int8_t pointer to the start of the space to
// copy to.
// 'st' is an int for adding to datatocopy to step along it (the number
// of 4bit blocks from the left (starting at 0)).
// 'st2' dito, but for tocopyto.
while( ((*(datatocopy + ((st % 2 ? st-1 : st)/2 ))) >> (st % 2 ? 0 :
4) & 0xf) != 0 ) { // steps along and reads 4bits at a time
*(tocopyto + ((st2 % 2 ? st2 - 1 : st2) / 2) ) |= 0xf << (st2
% 2 ? 0 : 4); // zero the 4 bits...
*(tocopyto + ((st2 % 2 ? st2 - 1 : st2) / 2) ) ^= 0xf << (st2
% 2 ? 0 : 4); // ...about to be written to
tmp = *(datatocopy + ((st % 2 ? st-1 : st)/2 )); // get the
8bit byte that includes required 4bits
if( st % 2 ) { // it's odd (required 4bits are on the right).
tmp |= 0xf0; // delete the,
tmp ^= 0xf0; // left 4bits.
} else // it's even (required 4bits on the left).
tmp >>= 4; // shift to the right.
*(tocopyto + ((st2 % 2 ? st2 - 1 : st2) / 2)) |= tmp << ( st2
% 2 ? 0 : 4); // copy the 4bits
st++, st2++; // increment 4bit block counters
}
i'm using an intermediate temperory variable half way through - is that
necessary? it just seems an awful lot to do what it's doing.
i'd just like to really consolidate it somehow - possible? easy?
pointers to how to?
any help much apprecaited.
thanks, ben.