fa***********@gmail.com schrieb:
This what I'm basically supposed to do. If i have a byte which looks
like this 00010000, i need to convert it to 11110000. That would mean
if any of the bit fields is 1....all the bit fields to the left of it
should be made 1.
Notes:
- bit-fields are something different in C.
- A byte can have more than 8 bits in C; in fact, it has CHAR_BIT
bits.
The above can be described as you did:
unsigned char Byte;
....
if (NumberOfBits(Byte) != 0)
{
Dest = FillWithOnesForUnsetHigherValueBits(Byte);
}
However, this leaves unspecified what happens if you have
two set bits:
00010010
Will this lead to
11110010
or
11111110
If the former is the case, the above is a good description.
If the latter is the case,
Dest = FillAllBitsOneWithZerosForUnsetLowerValueBits(Byte ) ;
may be a better description.
I tried using left shift n right shift operators but that didnt
help...any other suggesstions pls?
You can "set" bits with |=, you can "toggle" bits with ^=, you
can test bits with &, you can clear bits with &=, you can shift
bits with <</>>.
You can do this in many different ways.
If I were you, I'd start with writing a function to output unsigned
char values in binary representation, so you can see how your
operations affect the byte.
You will have to apply some of the bit operations so you can
display the byte; then, you can start with modifying the byte.
If you have trouble, copy&paste the _compilable_ code you have
and post it here. Explain what you expect and how your programme
did fall short.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.