2

I have an application that requires me to do some bit masking. The masking will check a string and depending on which bits in that string are high(1) or low(0) enter different methods.

The bits are impotent if they are low(0) and we assume if they are high(1) that everything is ok.

254 = 11111110 in binary. This is the mask I want to use to check the last bit,

void Main()
{
    ushort[] data = new ushort[]{254};

    int alertPosition = 251;
    int tempCriticalPosition = 247;
    int fan1FailurePosition = 253;
    ** int fan2FailurePosition = 254;

    int alarmCritialBit = (tempCriticalPosition & data[0]) == 0 ? 0 : 1;
    int fan1LedFlag = (fan1FailurePosition & data[0]) == 0 ? 0 : 1;
    **int fan2CameraFlag = (fan2FailurePosition & data[0]) == 0 ? 0 : 1;
    int alertFlag = (alertPosition & data[0]) == 0 ? 0 : 1;

    System.Console.WriteLine(alarmCritialBit);
    System.Console.WriteLine(fan1LedFlag);
    System.Console.WriteLine(fan2CameraFlag);
    System.Console.WriteLine(alertFlag);
} 

In this example, that is a fan2Failure. So The data in my array and my bit mask are the same but the result I get is 1. The result I get for doing the other checks is also 1 so I'm confused now as to how this works.

Everything I have looked up on-line recommends a similar approach, I thought that to check my data for a fan2failure I provided in my mask the string for a fan2failure and if the data and my mask where the same it would return 0?

I know this is something simple but i have looked online and cant see what it is I am doing wrong.

Thanks

Colm Clarke
  • 480
  • 1
  • 7
  • 23
  • 2
    Why shouldn't `fan2CameraFlag` be 1? `254 & 254 == 254`, and thus you get 1 from its expression. Is this wrong? – Lasse V. Karlsen Jul 28 '14 at 11:51
  • 2
    possible duplicate of [Using a bitmask in C#](http://stackoverflow.com/questions/3261451/using-a-bitmask-in-c-sharp) – Eugene Podskal Jul 28 '14 at 12:04
  • For the love of all things holey. PLEASE use an enum! For sanity. For type safety! For readability. PLEASE PLEASE PLEASE. – Aron Jul 28 '14 at 18:07

3 Answers3

1

Why using inverted bit masks?

What I usually do when checking for bits is to first define constants like this:

const uint fMyFlag = 1u<<0;
const uint fSecond = 1u<<1;

const uint mMyMask = 3u<<2;
const uint mOpt1   = 0u<<2;
const uint mOpt2   = 1u<<2;
const uint mOpt3   = 2u<<2;
const uint mRsvd   = 3u<<2;

and do these operations:

if((value&fMyFlag) != 0) flag_is_set();
if((value&fMyFlag) == 0) flag_is_not_set();
if((value&mMyMask) == mOpt1) option_one();

value |= fMyFlag; // set the flag
value &=~fMyFlag; // clear the flag
value = (value&~mMyMask) | mOpt2; // set option 2

NOTE: the above operations should work for enums as well ;)

firda
  • 3,268
  • 17
  • 30
0

To check a mask you must do :

(value & mask) == mask

So :

**int fan2CameraFlag = (fan2FailurePosition & data[0]) == fan2FailurePosition ? 0 : 1;
Troopers
  • 5,127
  • 1
  • 37
  • 64
0

in my opinion you are using the wrong value for mask.

For example:

ushort[] data = new ushort[] { 254 };  // 11111110
int fan1FailurePosition = 253; //11111101

You are expecting this to return false, but in fact you are doing an AND, so:

11111110
11111101  (AND)
--------
11111100  (WHICH IS TRUE)

To get the correct value you can simply set:

int fan1FailurePosition = 1; //0000001

Resulting in:

11111110
00000001  (AND)
--------
00000000  (WHICH IS FALSE)
tfcstack
  • 138
  • 1
  • 10