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.
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;
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, nor
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 as __persistent
.
bit
variable,
only the least significant bit (LSb) 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 Boolean Types).
All addresses assigned to bit
objects and the sections
that hold them will be bit addresses. These are calculated as the address of the
byte in which the bit resides multiplied by 8, added to the bit's offset within the byte.
For absolute bit
variables (see Absolute Variables), the address specified in code must
also be a bit address. Take care when comparing these bit addresses to byte addresses used
by all other variables.