2

Possible Duplicate:
what does |= (single pipe equal) and &=(single ampersand equal) mean in c# (csharp)

Here is the context where it is being used:

long dirtyFlag = 0x0000;

if (erd.SupervisorCompType != orgErd.SupervisorCompType) // change has been made?
{
   dirtyFlag |= 0x0001;
   // etc...
}
Community
  • 1
  • 1
Sam
  • 1,621
  • 3
  • 22
  • 30

6 Answers6

7

dirtyFlag |= 0x0001 is equivalent to dirtyFlag = dirtyFlag | 0x0001. The | operator is the bitwise OR operator. In your case, it sets the lowest binary bit. Some further examples:

1 | 2 = 3 (0001 | 0010 = 0011)
2 | 4 = 6 (0010 | 0100 = 0110)
5 | 1 = 5 (0101 | 0001 = 0101)
CrazyCasta
  • 26,917
  • 4
  • 45
  • 72
  • 4
    Can you get a binary bit from an ATM machine with your PIN number? :-) – paxdiablo Oct 12 '12 at 22:54
  • What does this have to do with ATM machines? – CrazyCasta Oct 12 '12 at 22:56
  • 3
    'Whoosh' goes the humour :-) A bit is a 'binary digit' hence 'binary bit' is redundant, much like 'automated teller machine machine' or 'personal identification number number'. – paxdiablo Oct 12 '12 at 23:07
  • 2
    @paxdiablo I find it humorous that I've grown so acucstsmoed to parsing horirble English that I didn't even notice any one of the three reudnadnt statements in your comment. – Rob Oct 12 '12 at 23:11
  • @paxdiablo It seemed like the OP might not know what bitwise operations are so I figured I'd be a little redundant to help him search for more information/trigger a memory of bitwise operations. – CrazyCasta Oct 13 '12 at 00:38
3
a |= b

is the same as

a = a | b

which is the same for a great many operators, like += or /=.

If you want to understand bitwise operators such as |, have a look at Bitwise operation and usage.

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
2

| is the bitwise OR operator. It takes two numeric values and compares them bit by bit and if either of the bits are equal to 1, then it returns 1 for that bit position

// Just as
myIntegerValue += 1;
// is the same as writing
myIntegerValue = myIntegerValue + 1;

// It follows that
dirtyFlag |= 0x0001;
// is the same as writing
dirtyFlag = dirtyFlag | 0x0001;

The dirtyFlag variable is keeping track of which fields were edited.

I would imagine that there is a series of values that are being compared. The dirtyFlag will let you know which values will need to be modified.

value0 = 0x0001 in hexadecimal =  1 = 00000001 in binary
value1 = 0x0002 in hexadecimal =  2 = 00000010 in binary
value2 = 0x0004 in hexadecimal =  4 = 00000100 in binary
value3 = 0x0008 in hexadecimal =  8 = 00001000 in binary
value4 = 0x0010 in hexadecimal = 16 = 00010000 in binary

So if you've modified fields 0 and 4, dirtyFlag will be

00010001 = 17 = 0x0011
JackAce
  • 1,407
  • 15
  • 32
1

It will perform a bitwise OR on your dirtyFlag, it is the same as doing

dirtyFlag = dirtyFlag | 0x0001;

So basicaly it is a short anotation,that stands for OR equals. Your dirtyFlag before the OR is 0x0000 and it performs an OR with 0x0001 as both your values are considered hex values it will do

0000
0001
----
0001

so basicaly it will set to 1 your flag,that's all that it does

sokie
  • 1,966
  • 22
  • 37
0

Or equals. This code will set the 1 bit.

Matthew
  • 24,703
  • 9
  • 76
  • 110
0

It means or equals

dirtyFlag = dirtyFlag | 0x0001;
corsiKa
  • 81,495
  • 25
  • 153
  • 204