|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
C# |= OperatorSaxena formulated on donderdag :
> Hi what are the usage scenerio of |= OR Assignment Operator You can use it is you are working with the individual bits of the values: int or some enum. int i = 9; i |= 3; // now i = 11 Hans Kesting Same as usual :
a <op>= b is just a notation for a = a <op> b Else you may want elaborate a bit (is this OR itself you are wondering about ? it does a bitwise or, allowing to make sure bits are set if they were not previously set and doesn't changing the value if already set). See : http://en.wikipedia.org/wiki/Bitwise_operation -- Patrice "Saxena" <Sax***@discussions.microsoft.com> a écrit dans le message de groupe de discussion : 1FE2EEDD-9293-4F20-9CFC-D48E92619***@microsoft.com...Show quoteHide quote > Hi what are the usage scenerio of |= OR Assignment Operator On Jul 9, 8:37 am, Saxena <Sax***@discussions.microsoft.com> wrote:
> Hi what are the usage scenerio of |= OR Assignment Operator a = a | b;is equiavilent to a |= b; Joe Cool wrote:
> a = a | b; Forgive my ignorance, but what does this mean? I can see that it’sassigning some value to “a†based upon the outcome of the RHS, but what does the RHS actually do? Could you give some examples, and possibly a scenario where this would be used? Cheers, -- Dylan Parry http://electricfreedom.org | http://webpageworkshop.co.uk “If I had my life to live over again, I'd be a plumber†– Einstein "Dylan Parry" <use***@dylanparry.com> wrote in message It operates on the values at the bit level. Let's say a = 16 and b = 3. news:h34qg0$bjn$1@news.eternal-september.org... >> a = a | b; > > Forgive my ignorance, but what does this mean? I can see that it's > assigning some value to "a" based upon the outcome of the RHS, but what > does the RHS actually do? Could you give some examples, and possibly a > scenario where this would be used? Don't think of them as 16 and 3, but rather envision their binary representation: 00010000 and 00000011. The | (pronounced "or" in this case) operator compares bits from the two numbers that occupy the same positions (bits are numbered right to left starting at 0) and outputs the result to the same position. Or is just a mathematical operation, like addition, except it has its own rules* and there's no concept of "carry." To do the math yourself, just stack the two numbers: 00010000 00000011 ======== 00010011 In the above case, the result is exactly the same as if you had added the numbers together (19). This is because there were no instances where the bits in a given position were 1 in both numbers. When that happens, you get a different result from addition. Take 12 and 9, for example: 00001100 00001001 ======== 00001101 The result is 13. Remember when I said there's no concept of "carry"? In addition, when the bits at position 3 are added, they would produce 10 and the 1 would carry to position 4. No such thing happens in an or operation. All that really results from or'ing 12 with 9 is that bit 0 gets set, and that in fact is or's entire purpose in life: to TURN BITS ON. Conversely, "and" is used to turn bits off, and "xor" is used to swap their state. * The rules are: 0 | 0 yields 0, any other combination yields 1, meaning if either bit or both are 1, the result is 1. Jeff Johnson wrote:
> It operates on the values at the bit level. Ah, I get it now. Thanks.… > * The rules are: 0 | 0 yields 0, any other combination yields 1, meaning if > either bit or both are 1, the result is 1. -- Dylan Parry http://electricfreedom.org | http://webpageworkshop.co.uk “If I had my life to live over again, I'd be a plumber†– Einstein Dylan Parry wrote:
> Joe Cool wrote: http://msdn.microsoft.com/en-us/library/kxszd0kx.aspx>> a = a | b; > > Forgive my ignorance, but what does this mean? I can see that it’s > assigning some value to “a†based upon the outcome of the RHS, but what > does the RHS actually do? Could you give some examples, and possibly a > scenario where this would be used? explains it. The short version is: integer types - bitwise OR bool - OR without shortcut Arne
Other interesting topics
ContextMenu Does Not Work?
Handlling different Data types linq to sql speed question What's out there for Linux? obtain member of Dictionary stored at lowest internal ordinal dll protect code Difficulty with Regex pattern to validate a string Input - String : Output - Binary Oct Dec Hex Inherit two classes. Is this possible? Problem with DateTimePicker Value property |
|||||||||||||||||||||||