Take a look at these codes :
const int a = 1 << 0; // = 1
const int b = 1 << 1; // = 2
const int c = 1 << 2; // = 4
const int d = 1 << 3; // = 8
int flag = 15; // = 1 + 2 + 4 + 8
int e1 = flag & a; // = 1
int e2 = flag & b; // = 2
int e3 = flag & c; // = 4
int e4 = flag & d; // = 8
bool b1 = e1 != 0; // = true
bool b2 = e2 != 0; // = true
bool b3 = e3 != 0; // = true
bool b4 = e4 != 0; // = true
Upper Codes in other way(simpler) :
const int a = 1 << 0; // = 1
const int b = 1 << 1; // = 2
const int c = 1 << 2; // = 4
const int d = 1 << 3; // = 8
int flag = 15; // = 1 + 2 + 4 + 8
bool b1 = (flag & a) != 0; // = true
bool b2 = (flag & b) != 0; // = true
bool b3 = (flag & c) != 0; // = true
bool b4 = (flag & d) != 0; // = true
I need a Reverse Formula to return flag
value when we set :
b1 , b2 , b3 , b4 variables and make them constant.
So we have 2*2*2*2 = 16
different situations. mean (true|flase)*(true|flase)*(true|flase)*(true|flase) = 16
for setting b
variables.
With which method we can figure out what should flag
be?
Mean :
const int a = 1 << 0; // = 1
const int b = 1 << 1; // = 2
const int c = 1 << 2; // = 4
const int d = 1 << 3; // = 8
int flag = ?;
bool b1 = (? & a) != 0; // = true > I WANT b1 True
bool b2 = (? & b) != 0; // = false > I WANT b2 False
bool b3 = (? & c) != 0; // = true > I WANT b3 True
bool b4 = (? & d) != 0; // = false > I WANT b4 False
For example i want b1 True , b2 False , b3 True , b4 False
. So What is the flag?
Or
I want b1 False, b2 False , b3 True , b4 False
. So What is the flag?
How did you find it and give me a method to calculate it!
In my example
flag
is 15 (sum of a+b+c+d) and in this way all b
variables are true.Related: Bitwise operations | And | Which situation is true or false in c#