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

1 Answer

0 votes
#include <stdio.h>

#define ONE   0x01
#define TWO   0x02
#define THREE 0x04
#define FOUR  0x08

void multiValueExample(int values) {
    if ((values & ONE) == ONE) {
        printf("ONE\n");
    }
    
    if ((values & TWO) == TWO) {
        printf("TWO\n");
    }

    if ((values & THREE) == THREE) {
        printf("THREE\n");
    }
    
    if ((values & FOUR) == FOUR) {
        printf("FOUR\n");
    }
}

int main() {
    multiValueExample(ONE | THREE | FOUR);
    
    return 0;
}



/*
run:

ONE
THREE
FOUR
 
*/

 



answered Jan 14, 2025 by avibootz
...