[SOLVED]Bitwise "Or" Question.

I'm working on Functions that need "Flags", just like MessageBox.
But, Let's assume i call a function like:
void Test(DWORD Flags)
Let's say i put "MB_OK | MB_ICONERROR" (From the MessageBox, just for testing)
How would i find each value? I know how Or, And, Xor works.
Last edited on
It's possible that each flag's value is a power of two, in which case one could easily determine the flags from expressing the resulting value as a sum of powers of two. I don't know the mechanics behind the WinAPI MessageBox, but that's just a possibility. :)

-Albatross
It's a nice idea. I may try giving an eye to the values of MB_OK, etc etc. and post here what i get. Thanks for the idea. EDIT: Yes, they use approximatively that idea. This may explain something:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#define MB_OK                       0x00000000L
#define MB_OKCANCEL                 0x00000001L
#define MB_ABORTRETRYIGNORE         0x00000002L
#define MB_YESNOCANCEL              0x00000003L
#define MB_YESNO                    0x00000004L
#define MB_RETRYCANCEL              0x00000005L
#define MB_CANCELTRYCONTINUE        0x00000006L


#define MB_ICONHAND                 0x00000010L
#define MB_ICONQUESTION             0x00000020L
#define MB_ICONEXCLAMATION          0x00000030L
#define MB_ICONASTERISK             0x00000040L

#define MB_USERICON                 0x00000080L

#define MB_DEFBUTTON1               0x00000000L
#define MB_DEFBUTTON2               0x00000100L
#define MB_DEFBUTTON3               0x00000200L
#define MB_DEFBUTTON4               0x00000300L

#define MB_APPLMODAL                0x00000000L
#define MB_SYSTEMMODAL              0x00001000L
#define MB_TASKMODAL                0x00002000L
#define MB_HELP                     0x00004000L

#define MB_NOFOCUS                  0x00008000L
#define MB_SETFOREGROUND            0x00010000L
#define MB_DEFAULT_DESKTOP_ONLY     0x00020000L

#define MB_TOPMOST                  0x00040000L
#define MB_RIGHT                    0x00080000L
#define MB_RTLREADING               0x00100000L

#define MB_TYPEMASK                 0x0000000FL
#define MB_ICONMASK                 0x000000F0L
#define MB_DEFMASK                  0x00000F00L
#define MB_MODEMASK                 0x00003000L
#define MB_MISCMASK                 0x0000C000L 

Depending on the "Section" there are various choices (MB_OK and MB_OKCANCEL cannot be used together), as section i mean: Buttons that will be shown, icon that will be shown...
Last edited on
Well, in this case each bit is treated separately. So I'd just do something like the following in your Test(DWORD ) function:

1
2
3
4
5
if (Flags & 0x1) do_something();
if (Flags & 0x2) do_something_else();
if (Flags & 0x4) do_something_further();
if (Flags & 0x8) do_another thing();
if (Flags & 0x10) do_yet_another_thing();
But with names for the flags, not magic numbers, of course!

1
2
3
4
5
if (Flags & MB_ICONERROR)
    handleIconError();
if (Flags & MB_OK)
    handleOk();
....


(And actions taken to a condition should be on a different line to the test for it, if you want the VC++ debugger or gdb to work properly.)
Last edited on
Perfect, now i have all I was looking for. Thank you!
Topic archived. No new replies allowed.