This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

C Equivalent of assembler instruction ANL

Assembler:
----------
CCAPM1 DATA 0C3h
ANL CCAPM1, #00dh

C equivalent ????
-----------------
sfr ccapm1 = 0xc3;

ccapm1 = ccapm1 & ~(0x0d);
or
ccapm1 = ccapm1 & 0x0d;
or
ccapm1 = ccapm1 && 0x0d;

Can some one shed a light on the correct interpretation??

Thanks.

  • "Can some one shed a light on the correct interpretation"

    What you need is a 'C' textbook!

  • Always a good idea to have a language reference handy. I imagine you can find a tutorial on the web with a bit of Googling.

    Here's an online reference:

    http://www-ccs.ucsd.edu/c/

    but it's just that -- a reference, and so may be short on explanation.

    C has both "bitwise" and "logical" AND and OR operators. The bitwise operators act on each bit in a type independently. The logical operators interpret the operands as Boolean values (0 is false, non-zero is true, which is 1) and then produce a result. To set and clear bits, you generally want the bitwise operators, which are the single-character version ('&' instead of '&&').

    ccapm1 = ccapm1 & 0x0d;

    is the match for

    ANL CCAPM1, #0dH

    It performs a bitwise AND of the value in ccamp1 with 0x0d, and then stores the result back into ccapm1.

    ccapm1 = ccapm1 && 0x0d;

    will probably not do what you want. If ccapm1 is non-zero, then the result of the logical expression is 1, which will be assigned to ccapm1. If ccapm1 is zero, it will remain zero.

    ~ is the bitwise NOT operator. It inverts all the bits in the operand. So

    ccapm1 = ccapm1 & ~0x0d;

    is the same as

    ccapm1 = ccapm1 & 0xf2;

    Since using a variable in this sort of expression and then reassigning the result is such a common thing to do, C provides a shorthand notation.

    ccapm1 &= 0x0d;

    means exactly the same thing as

    ccapm1 = ccapm1 & 0x0d;

    The usual idioms for setting and clearing bits are thus:

    var |= mask; // set 1 bits of mask in var
    var &= ~mask; // clr 1 bits in mask from var