5.3.2.1 Bit Data Types And Variables

The MPLAB XC8 C Compiler supports a single-bit integer type via the __bit type specifier.

Bit variables behave in most respects like normal unsigned char variables, but they can only contain the values 0 and 1. They provide a convenient and efficient method of storing flags, since eight bit objects are packed into each byte of memory storage. Operations on bit variables are performed using the single bit instructions (bsf and bcf) wherever possible.

__bit init_flag;

These variables cannot be auto or parameters to a function, but can be qualified static, allowing them to be defined locally within a function. For example:

int func(void) {
    static __bit flame_on;
    // ...
}

A function can return a bit by using the __bit keyword in the function’s prototype in the usual way. The returned value will be stored in the STATUS register carry flag.

It is not possible to declare a pointer to bit types or assign the address of a bit object to any pointer. Nor is it possible to statically initialize bit variables so they must be assigned any non-zero starting value (i.e., 1) in the code itself. Objects qualified bit will be cleared on startup, unless the object is qualified __persistent.

When assigning a larger integral type to a bit variable, only the least significant bit is used. For example, in the following code:

int data = 0x54;
__bit bitvar;
bitvar = data;

bitvar will be cleared by the assignment since the LSb of data is zero. This sets the __bit type apart from _Bool, which is a boolean type (See 5.3.3 Boolean Types).

All addresses assigned to bit objects and the sections that hold them will be bit addresses. For absolute bit variables (see 5.4.4 Absolute Variables), the address specified in code must be a bit address. Take care when comparing these addresses to byte addresses used by all other variables.