Advertisement

sending args via or ( | )

Started by May 09, 2001 08:47 PM
4 comments, last by EvilCrap 23 years, 9 months ago
hi i noticed that in some functions, such as some in the windows.h, u can send parameters with or ex... func( 5 | 7 | 8, g | j); how do i write a function to handle this? i know how to write a function to receive elipses using stdarg.h, is this similar?\ thanks!
No, they are different methods. You can look up the usage of the "..." parameter under va_start, va_end, and va_arg.

The "|" means OR; it combines bits in either variable (or in many cases, constants/macros). If either one''s bit is true, then the end result is true. You can use it like this (this is an example, there''s many more ways to use it, and this example isn''t really practical):
  #define MF_PRINT_NOTHING 0x0 // 0000 0000 0000 0000#define MF_PRINT_A 0x1       // 0000 0000 0000 0001#define MF_PRINT_B 0x2       // 0000 0000 0000 0010#define MF_PRINT_C 0x4       // 0000 0000 0000 0100#define MF_PRINT_D 0x8       // 0000 0000 0000 1000#define MF_PRINT_E 0x10      // 0000 0000 0001 0000void MyFunction(unsigned int what) {  if(what & MF_PRINT_A) putchar(''A'');  if(what & MF_PRINT_B) putchar(''B'');  if(what & MF_PRINT_C) putchar(''C'');  if(what & MF_PRINT_D) putchar(''D'');  if(what & MF_PRINT_E) putchar(''E'');}// ---MyFunction(MF_PRINT_C); // Output: "C"MyFunction(MF_PRINT_C | MF_PRINT_E); // Output: "CE"MyFunction(MF_PRINT_NOTHING); // Output: ""  


Resist Windows XP''s Invasive Production Activation Technology!
http://druidgames.cjb.net/
Advertisement
Well, my code got mangled, but I''m sure you can figure it out...

Resist Windows XP''s Invasive Production Activation Technology!
http://druidgames.cjb.net/
The bitwise | operator is evaluated before being passed as in as an argument. All expressions are evaluated first.

Your example above takes two arguments. So, write a function that takes two arguments.

_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
EvilCrap: This can be done, but it only works in certain cases. The example you gave, 5 | 6 | 7, is not going to work. If you combine those three values with a bitwise OR, you won''t be able to recover them. Combining flags with a bitwise OR works when no two of the flags ever have the same bit set to 1. If this condition is met, then you can extract flags by using a bitwise AND. For defining these flags, powers of 2 are usually used, because a power of 2 has only one of its bits set to 1. Thus, when using a 32-bit integer, you can use bitwise OR to combine up to 32 flags such that they can all be read back again. For example:

#define SAMPLEFLAGA 1
#define SAMPLEFLAGB 2
#define SAMPLEFLAGC 4
#define SAMPLEFLAGD 8
#define SAMPLEFLAGE 16

and so on. Then you could call a function requiring a series of flags by combining all the required flags into a single argument, like this:

foo(SAMPLEFLAGA | SAMPLEFLAGC | SAMPLEFLAGD);

Now inside the body of the function, if dwFlags is the name of the parameter for the function, and you want to check if one of the flags is set, you''d do something like this:

if (dwFlags & SAMPLEFLAGC)
{
// code to execute if flag C is set goes here
}

Sorry this is kind of a fast explanation, but hopefully it makes sense.

-Ironblayde
 Aeon Software

"In C we had to code our own bugs. In C++ we can inherit them." - Unknown


"Your superior intellect is no match for our puny weapons!"
Thanks guys!!
i understand the use of the bitwise operators

i just wasnt sure if the use of the or was overloaded in functions or somthing.
thanks again.

This topic is closed to new replies.

Advertisement