Microchip Technology XC8 Standard Compiler (Workstation) SW006021-1 SW006021-1 사용자 설명서
제품 코드
SW006021-1
C Language Features
2012 Microchip Technology Inc.
DS52053B-page 145
5.4.2.1
BIT DATA TYPES AND VARIABLES
The MPLAB XC8 C Compiler supports bit integral types which can hold the values 0
or 1. Single bit variables may be declared using the keyword bit, for example:
or 1. Single bit variables may be declared using the keyword bit, for example:
bit init_flag;
These variables cannot be auto or parameters to a function, but can be qualified
static
static
, allowing them to be defined locally within a function. For example:
int func(void) {
static bit flame_on;
// ...
}
A function may return a bit object by using the bit keyword in the function’s prototype
in the usual way. The 1 or 0 value will be returned in the carry flag in the STATUS reg-
ister.
in the usual way. The 1 or 0 value will be returned in the carry flag in the STATUS reg-
ister.
The bit variables behave in most respects like normal unsigned char variables, but
they may only contain the values 0 and 1, and therefore provide a convenient and effi-
cient method of storing flags. Eight bit objects are packed into each byte of memory
storage, so they don’t consume large amounts of internal RAM.
they may only contain the values 0 and 1, and therefore provide a convenient and effi-
cient method of storing flags. Eight bit objects are packed into each byte of memory
storage, so they don’t consume large amounts of internal RAM.
Operations on bit objects are performed using the single bit instructions (bsf and
bcf
bcf
) wherever possible, thus the generated code to access bit objects is very effi-
cient.
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. Bit objects will be
cleared on startup, unless the bit is qualified persistent.
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. Bit objects will be
cleared on startup, unless the bit is qualified persistent.
When assigning a larger integral type to a bit variable, only the LSb is used. For
example, if the bit variable bitvar was assigned as in the following:
example, if the bit variable bitvar was assigned as in the following:
int data = 0x54;
bit bitvar;
bitvar = data;
it will be cleared by the assignment since the LSb of data is zero. This sets the bit
type apart from the C99 Standard __Bool, which is a boolean type, not a 1-bit wide
integer. The __Bool type is not supported on the MPLAB XC8 compiler. If you want to
set a bit variable to be 0 or 1 depending on whether the larger integral type is zero
(false) or non-zero (true), use the form:
type apart from the C99 Standard __Bool, which is a boolean type, not a 1-bit wide
integer. The __Bool type is not supported on the MPLAB XC8 compiler. If you want to
set a bit variable to be 0 or 1 depending on whether the larger integral type is zero
(false) or non-zero (true), use the form:
bitvar = (data != 0);
The psects in which bit objects are allocated storage are declared using the bit
PSECT
PSECT
directive flag, see Section 6.4.9.3 “PSECT”. All addresses specified for bit
objects and psects will be bit addresses. Take care when comparing these addresses
to byte addresses used by all other variables.
to byte addresses used by all other variables.
If the xc8 flag --STRICT is used, the bit keyword becomes unavailable.