-2

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#
Community
  • 1
  • 1
SilverLight
  • 19,668
  • 65
  • 192
  • 300
  • 1
    What is different than your deleted question? – Eser May 24 '16 at 21:08
  • 5 and 4 respectively, but you already showed how to calculate it so why is this a question? – harold May 24 '16 at 21:10
  • [Asking the same question again, but with different words](http://meta.stackoverflow.com/questions/303253/asking-the-same-question-again-but-with-different-words)... – Jeff Mercado May 24 '16 at 21:31

1 Answers1

2

Make flag variable using bitwise-or (|) operations with the values you want to be set:

if you want b1 and b4 be true and b2 y b3 be false:

int flag = a | d; 

bool b1 = (flag & a) != 0;  // = true
bool b2 = (flag & b) != 0;  // = false
bool b3 = (flag & c) != 0;  // = false
bool b4 = (flag & d) != 0;  // = true 

If you want only b3 to be true:

int flag = c; 

bool b1 = (flag & a) != 0;  // = false
bool b2 = (flag & b) != 0;  // = false
bool b3 = (flag & c) != 0;  // = true
bool b4 = (flag & d) != 0;  // = false 

If you have bs values and want the flag:

int flag = (b1 ? a : 0) | (b2 ? b : 0) | (b3 ? c : 0) | (b4 ? d : 0);
Arturo Menchaca
  • 15,783
  • 1
  • 29
  • 53