4
votes

I am using a bitwise enum to return a value from a function

[Flags]
private enum PatientRecord { NoRecord=0x0, SameEnrollmentDate=0x1, SameScreeningDate=0x2 }

and have a function with

var returnVar = PatientRecord.NoRecord;
....
if (condition...)
{
   returnVar &= PatientRecord.SameEnrollmentDate;
}
return returnVar

The debugger is showing that returnVar has the same value before and after the AND assignment operator has executed (be that PatientRecord.NoRecord (0) or PatientRecord.SameScreeningDate (2)).

why is this, and are there any solutions neater than:

returnVar = returnVar & PatientRecord.SameEnrollmentDate;

Thanks.

1

1 Answers

4
votes

You want bitwise OR (|) rather than bitwise AND (&):

var returnVar = PatientRecord.NoRecord;
....
if (condition...)
{
   returnVar |= PatientRecord.SameEnrollmentDate;
   //        ^ Bitwise OR assignment
}

AND'ing with zero (NoRecord=0x0) will always result in zero.