# #define by orders of two in a clean fashion

This topic is 4625 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm doing some stuff with bit operations in C++, and I need to #define constants by orders of two. For example, say I had a char variable representing financial packages for students at college. They can be on a meal plan, a housing plan, a parking space plan, and/or a financial aid plan. If I want to store whether they are on these plans, I might #define them like this:
#define ON_MEAL_PLAN 1
#define ON_HOUSING_PLAN 2
#define ON_PARKING_PLAN 4
#define ON_FINANCIAL_PLAN 8

I would really rather #define these by orders of two; this is really just kind of a code formatting issue... I want it to look nice :) Any ideas? If you don't understand me, just ask and I'll try to clarify.

##### Share on other sites
You mean something like this?
enum {  ON_MEAL_PLAN = 1,  ON_HOUSING_PLAN = 2 << 0,  ON_PARKING_PLAN = 2 << 1,  ON_FINANCIAL_PLAN = 2 << 2};

EDIT: Err, this may look a bit better:
enum {  ON_MEAL_PLAN = 1 << 0,  ON_HOUSING_PLAN = 1 << 1,  ON_PARKING_PLAN = 1 << 2,  ON_FINANCIAL_PLAN = 1 << 3};

##### Share on other sites
Sure, I think that would work fine! Unless anyone else thinks of a nicer-looking way, I'll just go with that. Thanks.

##### Share on other sites
Quote:
 Original post by silverphyre673I'm doing some stuff with bit operations in C++, and I need to #define constants by orders of two. For example, say I had a char variable representing financial packages for students at college. They can be on a meal plan, a housing plan, a parking space plan, and/or a financial aid plan. If I want to store whether they are on these plans, I might #define them like this:#define ON_MEAL_PLAN 1#define ON_HOUSING_PLAN 2#define ON_PARKING_PLAN 4#define ON_FINANCIAL_PLAN 8I would really rather #define these by orders of two; this is really just kind of a code formatting issue... I want it to look nice :) Any ideas? If you don't understand me, just ask and I'll try to clarify.

One relatively common thing is to bit shift each one
#define ON_MEAL_PLAN      (1<<0)#define ON_HOUSING_PLAN   (1<<1)#define ON_PARKING_PLAN   (1<<2)#define ON_FINANCIAL_PLAN (1<<3)
The other method I see a lot is to just do what you have above
#define ON_MEAL_PLAN 1#define ON_HOUSING_PLAN 2#define ON_PARKING_PLAN 4#define ON_FINANCIAL_PLAN 8
You might want to consider an enum if you don't need them in the preprocessor. I'm not too familiar with bit fields, but if your implmentation packs them well (I don't know how common that is), then that may be an option as well.

##### Share on other sites
OK. Yeah, this is a total formatting issue, so I think the enumeration bit-shifting technique will work fine. I remember seeing this somewhere months ago online, but I'd forgotten. Thanks, all!

##### Share on other sites
you could always just make a set of standard defs
#define BIT1 (1<<0)
#define BIT2 (1<<1)
...

or
#define BIT(x) (1<<x)

and then just do
#define ON_MEAL_PLAN BIT(1)
#define ON_HOUSING_PLAN BIT(2)
...

##### Share on other sites
You usually see it as hex

enum MyEnum {
A = 0x01,
B = 0x02,
C = 0x04,
D = 0x08,
E = 0x10,
F = 0x20,
G = 0x40,
H = 0x80,
I = 0x100
}

Or bit-fields might be what you really want:

struct Stuff {
uint32 MealPlan :1;
uint32 HousingPlan :1;
uint32 ParkingPlan :1;
uint32 FinancialPlan :1;
};

unsigned bit-fields are technically non-standard, but I never see signed bit-fields in code.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633722
• Total Posts
3013547
×