using bits as flags

S

Scott Kelley

Wanted to create a 'flag byte' to use for a number of things. The following
seemed a reasonable approach:

#define connected = 0x01
unsigned char FlagByte;

why does this work:
FlagByte &= ~0x01; // set connected flag = 0

& this doesn't
FlagByte &= ~connected; // set connected flag = 0

Thanks,
Scott Kelley
 
M

Martin Dickopp

Scott Kelley said:
Wanted to create a 'flag byte' to use for a number of things. The following
seemed a reasonable approach:

#define connected = 0x01

This means that every occurrence of `connected' is replaced by `= 0x01'.
unsigned char FlagByte;

why does this work:
FlagByte &= ~0x01; // set connected flag = 0

& this doesn't
FlagByte &= ~connected; // set connected flag = 0

Think about what this becomes when `connected' is replaced (given what I
have written above), and you'll surely see what the mistake is. :)

Martin
 
M

Martin Ambuhl

Scott said:
Wanted to create a 'flag byte' to use for a number of things. The following
seemed a reasonable approach:

#define connected = 0x01
^^^

lose the bogus '='
 
P

Peter Nilsson

Scott Kelley said:
Wanted to create a 'flag byte' to use for a number of things. The following
seemed a reasonable approach:

#define connected = 0x01

#define connected 0x01u

Note: bit manipulations with signed (and hence possibly negative) quantities
are potentially problematic in ISO C.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,807
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top