# |'s in function arguments

• 11-05-2009
timmeh
|'s in function arguments
I've been using PDCurses in a rogue-like project recently, and one of the functions (in PDCurses) allows me to enter an arbitrary number of arguments, separated by a |. I.E. I could call
Code:

`attron(COLOR_PAIR(1) | A_BOLD);`
and all the output after it would be bold, and use color pair 1.

While it's nothing I need to use at the moment, it has me wondering how exactly one would write a function to take arguments like that. Google searching for it is difficult, since it seems to ignore the | in the search text. So any information or links to a good source where I can study up on this would be great.

Thanks,
Timothy
• 11-05-2009
brewbuck
It has nothing to do with calling a function. The '|' is just an operator like '+'.
• 11-05-2009
King Mir
It's a set of bit flags, that are OR-ed together.
For example:
Code:

```#define READ 0x1 #define WRITE 0x2 ... func(READ|WRITE)//call func with READ and WRITE flags.```
• 11-05-2009
timmeh
Okay, so I'm guessing if I were to pass a function (1|3) it would look (in bits at least) something like this: 3010?

If I were to pass that (1|3) as an integer to a function, could I check if a 3 was one of the arguments "OR-ed" by using
Code:

`if(3&my_int==3)`
or something?

• 11-05-2009
Sebastiani
Think of it like an array, or std::vector, except the 'cells' are the individual bits of the number; Using the OR operator is effectively "adding the value to the list", eg:

0000 0010 | 1000 0000 = 1000 0010

It's usually a good idea to memorize the standard logical operations, as well, eg:

OR
-----------
0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1

AND
-----------
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1

XOR
------------
0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0

NOT
------------
!0 = 1
!1 = 0

From those you can build any other possible logical operation (eg: NAND is !(A & B), etc).

Anyway, back to the original question, to undo the result of an OR, you would negate each bit and then AND it with the number, eg:

A |= B (set bits of B in A)
A &= ~B (remove bits of B in A)

Note that '~' corresponds to a logical '!' applied to each bit.
• 11-05-2009
Sebastiani
Quote:

Originally Posted by timmeh
Okay, so I'm guessing if I were to pass a function (1|3) it would look (in bits at least) something like this: 3010?

If I were to pass that (1|3) as an integer to a function, could I check if a 3 was one of the arguments "OR-ed" by using
Code:

`if(3&my_int==3)`
or something?