quik question about the '|' sign
Aug 31, 2013 at 12:21pm UTC
I know the that you can do someting like
getSomething(ONE_THING | ANOTHER_THING);
where you make use of the '|'.
But how do is recieve and use those multyple things?
Aug 31, 2013 at 12:35pm UTC
Aug 31, 2013 at 12:39pm UTC
For example that to check that at least one number of two numbers is non-zero.
1 2 3 4 5 6 7
int x = 0, y = 0;
std::cout << "Enter two numbers: " ;
std::cin >> x >> y;
if ( x | y ) std::cout << "At least one number is not equal to zero.\n" ;
else std::cout << "Both numbers are equal to zero.\n" ;
Aug 31, 2013 at 12:40pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
enum MyThings {
THING_ONE = 0x1,
THING_TWO = 0x2,
THING_THREE = 0x4,
THING_FOUR = 0x8,
THING_FIVE = 0x10,
THING_SIX = 0x20,
THING_SEVEN = 0x40,
THING_EIGHT = 0x80
};
int things = THING_ONE|THING_FOUR;
bool has_one = (things&THING_ONE) == THING_ONE;
bool has_two = (things&THING_TWO) == THING_TWO;
bool has_three = (things&THING_THREE) == THING_THREE;
bool has_four = (things&THING_FOUR) == THING_FOUR;
bool has_five = (things&THING_FIVE) == THING_FIVE;
bool has_six = (things&THING_SIX) == THING_SIX;
bool has_seven = (things&THING_SEVEN) == THING_SEVEN;
bool has_eight = (things&THING_EIGHT) == THING_EIGHT;
You cannot use arbitrary number.
You must use numbers who are made of a single binary bit activated.
E.G. :
Hex -> Binary
0x1 == 00000001
0x2 == 00000010
0x4 == 00000100
0x8 == 00001000
0x10== 00010000
0x20== 00100000
0x40== 01000000
0x80== 10000000
Aug 31, 2013 at 1:02pm UTC
ty so much guys
Aug 31, 2013 at 1:36pm UTC
@vlad: Your example also works with ||.
I think DutchMofo meant how to use bitmasks/bitwise operators.
Topic archived. No new replies allowed.