Keil Logo

Bit-Addressable Objects

Bit-addressable objects are objects that may be addressed as words or as bits. Only data objects that occupy the bit-addressable area of the 251 internal memory fall into this category.

The C251 Compiler places variables declared with the bdata or ebdata memory type into the bit-addressable area. The memory type bdata refers to the 8051 compatible bit space; ebdata specifies the extended 251 bit space. Both the bdata and ebdata memory type are handled like the data memory type except that variables reside in the bit addressable portion of the on chip 251 data memory. Note that the total size of this area of memory may not exceed 16 bytes in case of bdata or 96 bytes in case of ebdata.

You may declare global bdata/ebedata variables as shown below:

int bdata ibase;        /* Bit-addressable int */

char bdata bary [4];    /* Bit-addressable array */

int ebdata ibase;        /* Ebit-addressable int */

char ebdata bary [4];    /* Ebit-addressable array */

The variables ibase and bary are bit-addressable. Therefore, the individual bits of these variables may be directly accessed and modified. Use the sbit keyword to declare new variables that access the bits of bdata variables. For example:

sbit mybit0 = ibase ^ 0;      /* bit 0 of ibase */
sbit mybit15 = ibase ^ 15;    /* bit 15 of ibase */

sbit Ary07 = bary[0] ^ 7;     /* bit 7 of bary[0] */
sbit Ary37 = bary[3] ^ 7;     /* bit 7 of bary[3] */

sbit ebBit0 = ebase ^ 0;      /* bit 0 of ebase */
sbit ebBit15 = ebase ^ 15;    /* bit 15 of ebase */

sbit ebAry07 = eary[0] ^ 7;     /* bit 7 of eary[0] */
sbit ebAry37 = eary[3] ^ 7;     /* bit 7 of eary[3] */

The above example represents declarations, not assignments to the bits of the ibase/ebase and bary/eary variables. The expression following the carat symbol ('^') in the example specifies the position of the bit to access with this declaration. This expression must be a constant value.

The range depends on the type of the base variable included in the declaration. The range is:

  • 0-7 for char and unsigned char,
  • 0-15 for int, unsigned int, short, and unsigned short, and
  • 0-31 for long and unsigned long.


  • The C251 Compiler assumes that objects accessed using sbit declarations are stored in little endian byte order. This is the case for sfr16 types. However, standard C types like int and long are stored in big endian.

You may provide external variable declarations for the sbit type to access these types in other modules. For example:

extern bit mybit0;      /* bit 0 of ibase */
extern bit mybit15;     /* bit 15 of ibase */

extern bit Ary07;       /* bit 7 of bary[0] */
extern bit Ary37;       /* bit 7 of bary[3] */

extern bit ebdata eBit15;     /* external ebdata based bit */
extern bit ebdata ebAry37;    /* external ebdata based bit */

Declarations involving the sbit type require that the base object be declared with the memory type bdata or ebdata. Note that the declaration of external bits which are ebdata based require the memory type ebdata. Without the explicit memory, the default space bdata is assumed.

The only exceptions are the variants for special function bits. If you want to declare special function register based bits, the memory space must be omitted. Refer to Special Function Registers for more information.

The following example shows how to change the ibase and bary bits using the above declarations.

Ary37 = 0;        /* clear bit 7 in bary[3] */
bary[3] = 'a';    /* Byte addressing */
ibase = -1;       /* Word addressing */
mybit15 = 1;      /* set bit 15 in ibase */

The bdata memory type is handled like the data memory type except that variables declared with bdata reside in the bit-addressable portion of the internal data memory. Note that the total size of this area of memory may not exceed 16 bytes.

In addition to declaring sbit variables for scalar types, you may also declare sbit variables for structures and unions. For example:

union lft
  float mf;
  long ml;

bdata struct bad
  char m1;
  union lft u;
  } tcp;

sbit tcpf31 = ^ 31;        /* bit 31 of float */
sbit tcpm10 = tcp.m1 ^ 0;
sbit tcpm17 = tcp.m1 ^ 7;


  • You may not specify bit variables for the bit positions of a float. However, you may include the float and a long in a union and then declare bit variables to access the bits in the long type.
  • The sbit data type uses the specified variable as a base address and adds the bit position to obtain a physical bit address. Physical bit addresses are not equivalent to logical bit positions for certain data types.

    Physical bit position 0 refers to bit position 0 of the first byte. Physical bit position 8 refers to bit position 0 of the second byte. Because int variables are stored high-byte first, bit 0 of the integer is located in bit position 0 of the second byte. This is physical bit position 8 when accessed using an sbit data type.
  • Variables declared with the bdata and ebdata type must be global; it is not possible to define bit addressable objects inside a function
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.