BUG with bits in integer variable

There is a BUG in the compilation of a bit definition:

unsigned int bdata my_int;
sbit bit15 = my_int ^ 15;
sbit bit7 = my_int ^ 7;

void main()
{
  my_int = 0x8000;

  if(bit15)
  {
    // Here we shall not appear,as compiller
    // will be erroneous to check 7-bit of
    // a variable my_int instead of 15-bit
    // (as this variable place in memory in
    // little endian order)
    // ...
  }

  if(bit7)
  {
    // oops! we here!
    // ...
  }
}
This is a BUG! Please correct. Thanx.

Parents
  • hi,

    unsigned int bdata my_int;
    sbit bit15 = my_int ^ 15;
    sbit bit7 = my_int ^ 7;
    

    So what it does:

    - first line allocates two bytes of memory inside bit-addressable memory space to place integer number there and names variable as my_int;
    - second and third lines declare two variables: bit7 and bit15. These variables are defined as 7th and 15th bits of bit-addressable memory space related from address where variable my_int has been placed. Important note: lines like
    bitX = variable ^ X;
    HAVE NOTHING with the variable; they are related to the address where the variable is placed at. Due Keil uses big-endian storing format so it holds MSB of integer firstly then LSB.

    Bits in data of any width are always 0 - LSB, etc. up to MSB.

    The native C does not support bit variables at all. ANSI C supports bit-fields only. Such questions as bit-field placement, bits' order and how many bytes are utilized per bitfield - are not defined by ANSI C. It says: "compiler-dependence, read its manual".

    Now come back to your code. For me it is clean and should be translated to:
    void main()
    {
      my_int = 0x8000;
    
      if((my_int & 0x8000) != 0)  // check for bit 15
      {
      }
      if((my_int & 0x0080) != 0) // check bit 7
      {
      }
    }
    
    Regards,
    Oleg
    

Reply
  • hi,

    unsigned int bdata my_int;
    sbit bit15 = my_int ^ 15;
    sbit bit7 = my_int ^ 7;
    

    So what it does:

    - first line allocates two bytes of memory inside bit-addressable memory space to place integer number there and names variable as my_int;
    - second and third lines declare two variables: bit7 and bit15. These variables are defined as 7th and 15th bits of bit-addressable memory space related from address where variable my_int has been placed. Important note: lines like
    bitX = variable ^ X;
    HAVE NOTHING with the variable; they are related to the address where the variable is placed at. Due Keil uses big-endian storing format so it holds MSB of integer firstly then LSB.

    Bits in data of any width are always 0 - LSB, etc. up to MSB.

    The native C does not support bit variables at all. ANSI C supports bit-fields only. Such questions as bit-field placement, bits' order and how many bytes are utilized per bitfield - are not defined by ANSI C. It says: "compiler-dependence, read its manual".

    Now come back to your code. For me it is clean and should be translated to:
    void main()
    {
      my_int = 0x8000;
    
      if((my_int & 0x8000) != 0)  // check for bit 15
      {
      }
      if((my_int & 0x0080) != 0) // check bit 7
      {
      }
    }
    
    Regards,
    Oleg
    

Children
More questions in this forum