tags:

views:

281

answers:

6

While reading the Android guide to Notifications, I stumbled accross this:

Adding vibration

You can alert the user with the the default vibration pattern or with a vibration pattern defined by your application.

To use the default pattern, add "DEFAULT_VIBRATE" to the defaults field:

notification.defaults |= Notification.DEFAULT_VIBRATE;

What this does is clear: it adds the DEFAULT_VIBRATE flag to the default flags of the notification object. But what does the |= operator do in Java? It looks like an "OR", but how does it work?

Can you provide an example using numbers?

Thanks

+12  A: 

a |= x is a = a | x, and | is "bitwise inclusive OR"

Whenever such questions arise, check the official tutorial on operators.

Each operator has an assignment form:

+= -= *= /= %= &= ^= |= <<= >>= >>>=

Where a OP= x is translated to a = a OP x

And about bitwise operations:

   0101 (decimal 5)
OR 0011 (decimal 3)
 = 0111 (decimal 7)

The bitwise OR may be used in situations where a set of bits are used as flags; the bits in a single binary numeral may each represent a distinct Boolean variable. Applying the bitwise OR operation to the numeral along with a bit pattern containing 1 in some positions will result in a new numeral with those bits set. For example:

Bozho
That's what I did, but I could not find the |= form, that looks strange to me
bodom_lx
@bodom_lx well, it is there right there on the page I linked.
Bozho
it is not..There is the bitwise-inclusive or operator "|", which I know. What I did not know was the "|=" form. I saw it today for the first time in my life and it did not looked obvious for me and my computer science class mates
bodom_lx
1. open the page. 2 press CTRL+F in your browser 3. type |= 4. see, it's there. 5. it's under "assignment" and there is "assignment" in the menu on the left.
Bozho
Damn, you're right haha..sorry mate, 8 eyes failed in seing it. You deserve a +1 at least :)
bodom_lx
+1 For the generic `a OP= x` → `a = a OP x` explanation.
Gumbo
+1  A: 

This is the bit wise OR operator. If notifications.default is 0b00000001 in binary form and Notification.DEFAULT_VIBRATE is 0b11000000, then the result will be 0b11000001.

kgiannakakis
+3  A: 

It is a short hand notation for performing a bitwise OR and an assignment in one step.

x |= y is equivalent to x = x | y

This can be done with many operators, for example:

x += y
x -= y
x /= y
x *= y
etc.

An example of the bitwise OR using numbers.. if either bit is set in the operands the bit will be set in the result. So, if:

x = 0001 and
y = 1100 then
--------
r = 1101
mikecsh
+1  A: 

In this case, notification.defaults is a bit array. By using |=, you're adding Notification.DEFAULT_VIBRATE to the set of default options. Inside Notification, it is likely that the presence of this particular value will be checked for like so:

notification.defaults & Notification.DEFAULT_VIBRATE != 0 // Present
David Grant
+10  A: 

|= is a bitwise-OR-assignment operator. It takes the current value of the LHS, bitwise-ors the RHS, and assigns the value back to the LHS (in a similar fashion to += does with addition).

For example:

foo = 32;   // 32 =      0b00100000
bar = 9;    //  9 =      0b00001001
baz = 10;   // 10 =      0b00001010
foo |= bar; // 32 | 9  = 0b00101001 = 41
            // now foo = 41
foo |= baz; // 41 | 10 = 0b00101011 = 43
            // now foo = 43
Chris
thank you very much, this is clear and simple
bodom_lx
+1  A: 

bitwise OR operator

Kyran Miles
it is not _only_ the bitwise OR
Bozho