Menu

#107 Optimised bit clear with masks

open
nobody
None
5
2005-08-25
2005-08-25
No

C does not really have any bit operations, which is a
nuisance if the target microprocessor has bit
manipulation instructions. To set or clear a bit in a
byte the programmer may choose to go with the portable
C code or drop down into non-portable assembler. Eg.

char abyte;
const char bitOfInterestMask = 0x01;

/* set the bit */
abyte |= bitOfInterestMask;
/* clear the bit */
abyte &= ~bitOfInterestMask;

I noticed on the HC08 port that the set operation is
efficiently optimised to the bset instruction but the
clear operation isn't optimised to the bclr instruction.

Discussion

  • Erik Petrich

    Erik Petrich - 2005-08-28

    Logged In: YES
    user_id=635249

    The code generator already supports using bclr this way.
    Could you post a compilable example in which |= reduces to
    bset, but &= does not reduce to bclr? I tried your code
    snippet in a number of different contexts and found that
    either both bit set and clear would be optimized or neither
    would.

     
  • Oliver Sedlacek

    Oliver Sedlacek - 2005-08-29

    Logged In: YES
    user_id=1316375

    I've uploaded the C source of an example. The code snippet
    of significance is lines 257 and 258,
    if(hectoRPM.b[U16_MSB] >
    miscCfg.stc.REV_LIMITER) PORTB |= MskPBRevLmt;
    else PORTB &= ~MskPBRevLmt;
    This was compiled with version 2.5.0 using
    sdcc -c -mhco8 measure.c

     
  • Oliver Sedlacek

    Oliver Sedlacek - 2005-08-29

    Example C source

     
  • Frieder Ferlemann

    Logged In: YES
    user_id=589052

    the mcs51 port doesn't always use bitset/bitclear operations
    either:

    #include <8052.h>
    void main(void)
    {
    /* P0 is a bit-addressable SFR (an SFR
    which is dividable by 8 without rest) */
    P0 |= 0x01;
    }

     

Log in to post a comment.