How to use a bitwise operator to pass multiple Integer values to a function for C#

1 Answer

0 votes
using System;

class Program
{
    const int ONE = 0x01;
    const int TWO = 0x02;
    const int THREE = 0x04;
    const int FOUR = 0x08;

    static void MultiValueExample(int values) {
        if ((values & ONE) == ONE) {
            Console.WriteLine("ONE");
        }

        if ((values & TWO) == TWO) {
            Console.WriteLine("TWO");
        }

        if ((values & THREE) == THREE) {
            Console.WriteLine("THREE");
        }

        if ((values & FOUR) == FOUR) {
            Console.WriteLine("FOUR");
        }
    }

    static void Main()
    {
        MultiValueExample(ONE | THREE | FOUR);
    }
}



/*
run:

ONE
THREE
FOUR
   
*/

 



answered Jan 14, 2025 by avibootz
...