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

Bitwise logical AND stores in a bit ....

Hi,

I get a curious result when compiling such a following code :

typedef union  {

  unsigned char                cCtrlFullByte;

  struct {
    unsigned char  bEnable          : 1;
    unsigned char  cUnused          : 7;

  }  cCtrlStruct;
} CtrlUnion;

void main (void)  {

    unsigned char  dummy = 0x55;
    CtrlUnion  xdata    bitUnion;

    bitUnion.cCtrlStruct.bEnable = dummy & 0x40;

    return;
}


It results in :
MOV A,#0x55
ANL A,#0x00
MOV R7,A
MOV DPTR, #0x0000
MOVX A,@DPTR
ANL A,#0xFE
ORL A,R7
MOVX @DPTR, A 

I thought that the bit result of bitwise logical AND is 1 if result is not 0, else 0.
It seems that I didn't understand ANSI the same way than Keil compiler ? Am I wrong ?

Arnaud DELEULE

Parents
  • I recently invented this macro to access Bits in a Byte:

    #define BITREF(aByte,aPos)((struct {unsigned char _0:1;unsigned char _1:1;unsigned char _2:1;unsigned char _3:1;unsigned char _4:1;unsigned char _5:1;unsigned char _6:1;unsigned char _7:1;}*)&aByte)->_##aPos
    

    which even works as l-value with XDATA
    (that's the reason, why I made it):

    byte xdata b;
    BITREF(b,4) = 1;
    BITREF(b,7) = BITREF(b,4);
    

    Arnaud DELEULE is right.

    I looked up the specification of the bit data type.
    The difference between variable of type bit and
    a struct-member of size 1bit is not documented.
    So to me it doesn't seem consistant too.

    Norbert

Reply
  • I recently invented this macro to access Bits in a Byte:

    #define BITREF(aByte,aPos)((struct {unsigned char _0:1;unsigned char _1:1;unsigned char _2:1;unsigned char _3:1;unsigned char _4:1;unsigned char _5:1;unsigned char _6:1;unsigned char _7:1;}*)&aByte)->_##aPos
    

    which even works as l-value with XDATA
    (that's the reason, why I made it):

    byte xdata b;
    BITREF(b,4) = 1;
    BITREF(b,7) = BITREF(b,4);
    

    Arnaud DELEULE is right.

    I looked up the specification of the bit data type.
    The difference between variable of type bit and
    a struct-member of size 1bit is not documented.
    So to me it doesn't seem consistant too.

    Norbert

Children