C Bit Access

Status
Not open for further replies.

Suraj143

Active Member
I have a variable called temp1.I need to check its bits individually.

The below code works for SFR.
Code:
if(PORTB.B0==1){.....}

But it won't work for GP registers.It gives an error "Operator '.' is not applicable to these operands 'temp1'"

Code:
if(temp1.B0==1){.....}
 
Last edited:
The generic way to check a bit is with a bitwise and.

Code:
if(temp1&0x01){.....}
or in binary
if(temp1&0b00000001){.....}

Some embedded c compilers will let you do a struct with bit fields. The definitions in the C18 processor header files are an example of that.

Code:
extern          near struct {
  unsigned C:1;
  unsigned DC:1;
  unsigned Z:1;
  unsigned OV:1;
  unsigned N:1;
} STATUSbits;

But I if you stick with the method above it will work on all c compilers.
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…