For bit shifting, as per your first example, use the <<
operator. In the C language there is no wrap-around for shifts, often termed as rotates. You would have to implement the operation yourself:
unsigned char carry;
carry = byte & 0x80; // Save the Most Significant bit for 8-bit byte.
byte <<= 1; // Left shift by one, insert a 0 as the least significant bit.
byte |= carry; // Put the rotated bit back into the byte.
Some processors also have a rotate through carry operation which will rotate the carry value in the next shift. This assumes that the carry
be a global variable.
To test bits in the C language, you will use the &
(binary AND operator) and maybe the ~
operator (negate). To test the most significant bit in an 8-bit byte:
if (byte & 0x80)
{
// bit is a 1
}
else
{
// bit is a 0
}
With all that said, you will have to find out why the carry flag (C_FLAG
) is used and design a different system around it. Generally, the carry bit is invalid outside of the assembly language function that it is used in. Some tightly coupled assembly language functions may violate this rule. In that case, rewrite the assembly language rather than debugging it. Redesign the whole program!