#### Archived

This topic is now archived and is closed to further replies.

# |= &= | & Operators

This topic is 5935 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Sup all, what do these operators; |= &= | & really do, i mean i kno that in DirectX for example FULLSCREEN | EXCLUSIVE, means that u want them both, but they r written to a DWORD (unsigned long), but how do you read that off again? how do you determind whether FULLSCREEN or EXCLUSIVE are selected? its got something to do with defining 1,2,4,8 etc etc... So any ideas? Yratelev

##### Share on other sites
& is bitwise AND
| is bitwise OR

1010 & 1100 = 1000
1010 | 1100 = 1110

Of course, you know binary 0001 is 1, binary 0010 is 2, 0100 is 4, 1000 is 8, etc etc... So instead of writing the flag in binary form, they write it in decimal.

For instance, you could decide the last bit means "EXCLUSIVE" (0001 = 1), and the previous means "FULLSCREEN" (0010 = 2)...
So you''ll define EXCLUSIVE 1 and FULLSCREEN 2.
Then, you''ll have EXCLUSIVE | FULLSCREEN = 0001 | 0010 = 0011

To check is a flag is set, you just do the following:

((Variable & FLAG) == FLAG)

So in that case, 0011 (Variable) & 0001 (Flag) = 0001 == Flag so the FULLSCREEN flag is set.

Cheers, ToohrVyk

##### Share on other sites
|= aggregate bitwise-or
&= aggregate bitwise-and
| bitwise-or
& bitwise-and

example
byte byte1 = 0x55; // 01010101b
byte byte2 = 0xaa; // 10101010b
byte result;

result = byte1 & byte2; // result = 00000000b
result = byte1 | byte2; // result = 11111111b

byte1 &= byte2; // byte1 = 00000000b
byte1 = 0x55; // reset byte1
byte2 |= byte1; // byte2 = 11111111b

Bogart

##### Share on other sites
Those are bitwise and bitwise assignment operators.

| - this is "or" - it combines the bits from two integers
& - this is "and" - it filters the bits of two integers against each other

|= - this assigns the resulting "or"''d bits to the variable on the left hand side of the expression - ie
x |= y is equivalent to x = x | y

&= - this assigns the resulting "and" bits to the variable on the left hand side of the expression - ie
x &= y is equivalent to x = x & y

Here are some notes on bitwise operations

BITWISE OPS : merging ops require data items to be of the same type (ie. have the same number of bits)operates on each corresponding bit in each data item in turn to yield a third bit in the resulting data itemeach bit is independent of the others - there is no arithmetical carry over from bit column to bit columnAND &- operates like a 2 bit truth table- if either bit is zero, yield bit is zero too- both bits must be one for the yield bit to be one- x & 0 = 0 | x & 1 = x	0011      & 0101	----	0001	XOR ^- bitwise exclusive OR- returns 1 when either bit but not both are 1- an XOR applied twice yields the original operand	0011      ^ 0101	----	0110OR |- two bits ORed yield 1 if either or both bits are 1 and 0 if both bits are 0- used to combine bits from different variables into a single variable	0011      | 0101	----	0111

##### Share on other sites
ahhhhhhhhhh it all seems so clear now cheers guys,
just one last thing, how does VC++ know the difference between

^ bit XOR, and ^ exponenecial?

dont worry if u cant be arsed to answer that, the above was plenty to answer my question!

Yratelev

##### Share on other sites
The C++ compiler knows the difference between ^ XOR and ^ exponential because ^ does not mean exponential in C++ :

double somevar = 2^2;

that code will not assign "somevar" the value 4 as you might think, it will assign "somevar" 0 because 2 xor 2 is 0 (any number xor''ed by itself equals to zero). To do exponential math in C++ you either need to write your own function or use one of the build in ones. I think it''s called pow or power or something like it. Writing your own should no be to hard neway.

Real programmers don''''t document, if it was hard to write it should be hard to understand

##### Share on other sites
It''s called "pow" it''s part of the run time library. Sorry I know you didn''t ask about XOR, but it was there in my notes along with the AND and OR and it''s another bitwise operator so I tossed it in for completeness. To follow up, the last bitwise operator is the ~ (tilde). It''s called a bitwise complement operator. It''s a unary operator that inverts the bits of the variable. So ~0101 is equivalent to 1010.

##### Share on other sites
is there a ''subtract'' operator, like:

1100 (sub) 0100 = 1000

im really learning stuff today!

Yratelev

##### Share on other sites
Yes, it''s -.

I think what you mean is some way to set the bits in the second argument that are one to zero, if they aren''t already, in the first.

There isn''t a separate operator, but you can use:

1100 & ~0100 = 1000

##### Share on other sites
(~ is the bitwise not operator)

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 12
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633723
• Total Posts
3013545
×