Jo*********@gmail.com wrote:
If I have three unsigned char(8 bits) and I want to compare the
xth bit in each. So if I had.
a 00000000
b 10101010
c 00000000
If I wanted to get the third bit from (a) the 0th bit from (b) and
compare them and store the result in the 7th bit of (c) how could
i go about doing this.
a 000[0]0000
b [1]0101010
c 0000000[0]
You could simply write:
putbit(7, c, (getbit(3, a) == getbit(0, b)));
which reduces the problem to writing putbit() and getbit(). After
you solve that you may decide to encapsulate getbit and putbit into
macros, but that is a separate decision. So start with the
frameworks:
void putbit(int bitno, char *byte, int bitvalue) (...}
int getbit(int bitno, char *byte) { ... }
or whatever other similar set tickles your fancy. That will
solidify your understanding of the range and purpose of bitno and
bitvalue. The latter is likely to be most useful if limited to 0
and 1, and similarly for the return values from getbit. With C99
you can use the _Bool type here (or bool after #include
<stdbool.h>). With C90 you would be advised to emulate the C99
techniques with compatible #defines.
--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!