# c++ enum and macros

## Recommended Posts

I understand things like: #define max(x,y) (x > y ? x : y) but what the heck does this mean? I'm completely lost
Quote:
 #define ARGB(a,r,g,b) ((DWORD(a)<<24) + (DWORD(r)<<16) + (DWORD(g)<<8) + DWORD(b)) .. .. // and another here #define GETB(col) ((col) & 0xFF)
I think it's the double '<<' that's confusing the hell out of me. Any help would be appreciated. The other question I had is about enums. I learned that if you have for instance, "enum {Joe, Moe, Cole}" that anytime you use for instance, Joe, it would get replaced by 0.. Moe by 1 and Cole by 2. However, when it comes to a big enum list like this how do you manage this?
Quote:
 enum hgeHwndState { HGE_HWND = 26, // int window handle: read only HGE_HWNDPARENT = 27, // int parent win handle (default: 0) HGEHWNDSTATE_FORCE_DWORD = 0x7FFFFFFF };
This is where I'm completely lost. Would anyone be willing to explain to me the more advanced part of macros and enum? Or at least post a link to as where you learned this stuff (most of the site I've searched don't get this detailed). Thanks for your time, and your help would be well appreciated.

##### Share on other sites
In this case << is a bitwise shifting operator.

For the ARGB macro, it is taking each parameter and shifting them into a different order, moving the a to the first 8 bits then the r to the second 8 bits etc.

Regarding the enum, i'm not sure what you mean by "how do you manage this". Can you explain please?

Hope that helps,

Dave

##### Share on other sites
"<<" and ">>" are bit-wise shift operators. x << n returns the value of x shifted left n bits and is equivalent to x * 2n. x >> n returns the value of x shifted right n bits and is equivalent to x / 2n. For example 5 << 2 == 20 (00000101 -> 00010100) and 48 >> 3 == 6 (00110000 -> 000000110).

Enumeration constants can be given specific values. The default is to start at 0 and increase by 1 for each one. Your example could have also been written,
    enum    {        Moe = 1,        Joe = 0,        Cole = 2    }
The values can be in any order, they don't have to be sequential and can even be duplicated. The enumeraton you are asking about simply has 3 values: 26, 27, and 0x7FFFFFFF.

##### Share on other sites

the enums part I get now .. can't believe it was that easy. However, as for the macros I'm still kind of lost on the bitwise stuff. Do you guys know of a good website that can explain this? I'm not saying your examples were bad. It's just that I need a more detailed explanation.

##### Share on other sites
Quote:
Original post by Instigator
but what the heck does this mean? I'm completely lost
Quote:
 #define ARGB(a,r,g,b) ((DWORD(a)<<24) + (DWORD(r)<<16) + (DWORD(g)<<8) + DWORD(b))
There are two things going on here: the bit twiddling, and the use of macros.

The first step is to forget you ever saw '#define...' (at least in this context). Instead, we'll write a function:
// Typed into post (no guarantee of correctness):DWORD ARGB(DWORD a, DWORD r, DWORD g, DWORD b) {    return (a << 24) + (r << 16) + (g << 8) + b;}
As for the bit manipulation, there's probably no really short explanation, so I'd suggest re-reading the previous posts and perhaps doing some research online, and then posting back if you have specific questions.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627711
• Total Posts
2978763

• 21
• 14
• 12
• 41