operator |=

C

Chinna Karuppan

Hi,
I am trying to under this operator |=.Has anybody got some documentation
/ online link to explain this....




THnks
Chinna
 
R

Rob Biedenharn

Hi,
I am trying to under this operator |=.Has anybody got some
documentation
/ online link to explain this....

THnks
Chinna


That's one of the assignment operators:

a |= b

is the same as

a = a | b

with all the regular message sending semantics. Look for the | method
of the class of a.

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
7

7stud --

Chinna said:
Hi,
I am trying to under this operator |=.Has anybody got some documentation
/ online link to explain this....




THnks
Chinna

The number 10 written in binary format is:

8's 4's 2's 1's
--- --- --- ---
1 0 1 0


The number 11 written in binary format is:

8's 4's 2's 1's
--- --- --- ---
1 0 1 1


Assume 1 is equal to true and 0 is equal to false. Now OR each column
together:

1 0 1 0
1 0 1 1 OR
----------
<--result?

In the first column(the leftmost column) you have (true OR true), which
is true, i.e you write 1 for the result:

1 0 1 0
1 0 1 1 OR
----------
1 <--result?

In the second column, you have (false OR false), which is false, i.e you
write a 0 for the result:

1 0 1 0
1 0 1 1 OR
----------
1 0 <--result?

In the third column, you have (true OR true), which is true:


1 0 1 0
1 0 1 1 OR
----------
1 0 1 <--result?

In the last column, you have (false OR true) which is true:

1 0 1 0
1 0 1 1 OR
----------
1 0 1 1 <--result?

What number is that?

8's 4's 2's 1's
--- --- --- ---
1 0 1 1


1x8 + 0x4 + 1x2 + 1x1 = 11. So 10 | 11 is equal to 11.

The syntax a |= b is similar to the syntax a += 1. a += 1 is equivalent
to a = a + 1. So a |= b is equivalent to a = a | b. From the above you
should now know how to OR two numbers together: convert the numbers to
binary format and then OR the columns together.
 
P

Paul McMahon

From the above you
should now know how to OR two numbers together: convert the numbers to
binary format and then OR the columns together.

Note that in ruby | is just a method. For integers it gives the result of
bitwise or, but other classes define it differently. For instance, Array
defines it to return set union, so [1] | [2] is [1,2].
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,285
Messages
2,571,416
Members
48,107
Latest member
jigyasauniversity

Latest Threads

Top