• C

Using bitwise operators to create boolean flags

I recently learned how to use bitwise operators to create 8 boolean flags using a single variable.  The purpose was to create a character map where I could define customized qualities, similar to the built-in lib functions like isalpha, isdigit, etc.

But suppose I want to have 16 flags?  I tried using a short instead of a char so I get 2 bytes to work with, and thus 16 bits to use as boolean flags.

But, I must be doing something wrong because when I change use a short array rather than a char array, I get results which are off sometimes.

Here I have 10 customized flags:

#define ALPHA 0x0001
#define NUMBER 0x0002
#define UPPER 0x0004
#define LOWER 0x0008
#define PUNCT 0x0010
#define DELIM 0x0020
#define STOP 0x0040
#define SPACE 0x0080
#define CURRENCY 0x8000
#define ELEMENT 0x0103

Then, I go through the first 127 characters like:

unsigned short __charmap[] = {
   DELIM | STOP,
   DELIM | ELEMENT,

...etc.

Yet, it doesn't work properly.  Can anyone spot something I'm doing wrong?
chsalviaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Harisha M GEngineerCommented:
Hi, try having 4 more zeroes at the beginning.. as in

#define ALPHA 0x00000001
#define NUMBER 0x00000002




---
Harish
chsalviaAuthor Commented:
Nevermind -

I just had the hex values wrong.  It should be:

#define ALPHA 0x0001
#define NUMBER 0x0002
#define UPPER 0x0004
#define LOWER 0x0008
#define PUNCT 0x0010
#define DELIM 0x0020
#define STOP 0x0040
#define SPACE 0x0080
#define CURRENCY 0x0100
#define ELEMENT 0x0200

x4uCommented:
The value 0x0103 that you use for ELEMENT has 3 bits set, the 0x0100, 0x0001 and 0x0002 bits. This means every ELEMENt would always have the ALPHA and NUMBER bit set as well.

You should better define the new values like this:
#define CURRENCY 0x0100
#define ELEMENT 0x0200

This way you are using seperate bits for each property.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
x4uCommented:
Yes ;-)
leisnerCommented:

#define ALPHA 0x0001
#define NUMBER 0x0002
#define UPPER 0x0004
#define LOWER 0x0008
#define PUNCT 0x0010
#define DELIM 0x0020
#define STOP 0x0040
#define SPACE 0x0080
#define CURRENCY 0x8000
#define ELEMENT 0x0103

It also is a better idea to have this as
#define ALPHA (1 << 0)
#define NUMBER (1 << 1)
#define UPPER  (1  << 2)
...
#define  CURRENCY (1 << 15)
And in this case:
#define ELEMENT ((1 << 8) + 0x3)

If element contains only 1 bit, you see something is wrong....
When you start using 32 bit flags, it easier to see what your doing  -- a common programming mistake
is miscounting your places....with the shift syntax, you don't mistake (but you may miscompute!)

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.