Sign in to follow this  

#define by orders of two in a clean fashion

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by silverphyre673
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.


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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this