Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitwise or (|) in function argument

I was wondering how to do this:

func(param1|param2|param3)

and then extract those values in the function, I have seen this in multiple functions, or is it better to do this:

func(param1, ...)

?

I am trying to do this in C++, and I was thinking of having the parameters to the function as values in a enum.

How do I solve this?

like image 673
DarkRoast Avatar asked Jan 08 '12 13:01

DarkRoast


People also ask

Is || A bitwise operator?

The bitwise operators should not be used in place of logical operators. The result of logical operators (&&, || and !) is either 0 or 1, but bitwise operators return an integer value. Also, the logical operators consider any non-zero operand as 1.

What is a bitwise inclusive or?

The bitwise inclusive OR operator ( | ) compares each bit of its first operand to the corresponding bit of its second operand. If either bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0. Both operands to the operator must have integral types.

What does |= mean in bitwise operators?

The bitwise OR assignment operator ( |= ) uses the binary representation of both operands, does a bitwise OR operation on them and assigns the result to the variable.

What is the >>> bitwise operator in Java?

In Java, bitwise operators perform operations on integer data at the individual bit-level. Here, the integer data includes byte , short , int , and long types of data. There are 7 operators to perform bit-level operations in Java.


2 Answers

Param1, param2, param3 are usually defined as a numbers with different bits turned on. | is an operator of bitwise alternative, that means it works on separate bits.

In example:

const int param1 = 0x01;
const int param2 = 0x02;
const int param3 = 0x04;

When you pass an argument to function you make a bitwise alternative of earlier defined params. In function, you don't make a decomposition, but check if a specified bit is set on, using bitwise conjunction:

void func(int arg){
  if(arg & param1)
    // do something
  if(arg & param2)
    // do something else
    // ...
}

func(param1 | param3); 
// "do something" will be done,
// but "do something else" not.
like image 180
Rafał Rawicki Avatar answered Oct 09 '22 19:10

Rafał Rawicki


This is an useful example to how create "MessageBox" function with arbitrary button types :

enum Button
{
    OK =      0x0001,
    CANCEL =  0x0010,
    YES =     0x0100,
    NO =      0x1000
};

void messagebox(char *text, int button)
{
    char msg[256];
    strcpy(msg, text);

    if ((button & Button::OK) == Button::OK)
        strcat(msg, " [OK]");
    if ((button & Button::CANCEL) == Button::CANCEL)
        strcat(msg, " [Cancel]");
    if ((button & Button::YES) == Button::YES)
        strcat(msg, " [Yes]");
    if ((button & Button::NO) == Button::NO)
        strcat(msg, " [No]");

    cout << msg << endl;
}

int main()
{
    messagebox("salam", Button::OK | Button::CANCEL);
    return 0;
}