?
Solved

Using bitwise operators to create boolean flags

Posted on 2006-03-26
5
Medium Priority
?
294 Views
Last Modified: 2010-04-15
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?
0
Comment
Question by:chsalvia
5 Comments
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16293734
Hi, try having 4 more zeroes at the beginning.. as in

#define ALPHA 0x00000001
#define NUMBER 0x00000002




---
Harish
0
 

Author Comment

by:chsalvia
ID: 16293805
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

0
 
LVL 11

Accepted Solution

by:
x4u earned 500 total points
ID: 16293814
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.
0
 
LVL 11

Expert Comment

by:x4u
ID: 16293820
Yes ;-)
0
 
LVL 3

Assisted Solution

by:leisner
leisner earned 500 total points
ID: 16293962

#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!)

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question