Microchip Technology XC8 Standard Compiler (Workstation) SW006021-1 SW006021-1 ユーザーズマニュアル

製品コード
SW006021-1
ページ / 518
How To’s
 2012 Microchip Technology Inc.
DS52053B-page 51
3.4.4.2
HOW CAN I ACCESS INDIVIDUAL BITS OF A VARIABLE?
There are several ways of doing this. The simplest and most portable way is to define 
an integer variable and use macros to read, set or clear the bits within the variable 
using a mask value and logical operations, such as the following.
#define  testbit(var, bit)   ((var) & (1 <<(bit)))
#define  setbit(var, bit)    ((var) |= (1 << (bit)))
#define  clrbit(var, bit)    ((var) &= ~(1 << (bit)))
These, respectively, test to see if bit number, bit, in the integer, var, is set; set the 
corresponding bit in var; and clear the corresponding bit in var. Alternatively, a 
union of an integer variable and a structure with bit-fields (see Section 5.4.4.2 
“Bit-Fields in Structures”
)
 can be defined, e.g.
union both {
unsigned char byte;
struct {
unsigned b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1;
} bitv;
} var;
This allows you to access byte as a whole (using var.byte), or any bit within that vari-
able independently (using var.bitv.b0 through var.bitv.b7).
Note that the compiler does support bit variables (see Section 5.4.2.1 “Bit Data Types 
and Variables”
) as well as bit-fields in structures.
3.4.4.3
HOW LONG CAN I MAKE MY VARIABLE AND MACRO NAMES?
The C Standard indicates that a only a number initial characters in an identifier are sig-
nificant, but it does not actually state what this number is and it varies from compiler to 
compiler. For XC8, the first 255 characters are significant, but this can be reduced 
using the -N option, see Section 4.8.9 “-N: Identifier Length”. The few character 
there are in your variable names, the more portable your code. Using the -N option 
allows the compiler to check that your identifiers conform to a specific length. This 
option affects variable and function names, as well as preprocessor macro names.
If two identifiers only differ in the nonsignificant part of the name, they are considered 
to represent the same object, which will almost certainly lead to code failure.