-3

I see constructs similar to the following a lot (in android since I do mostly android in Java)

final WindowManager.LayoutParams paramsWM
...
paramsWM.gravity = Gravity.TOP|Gravity.LEFT;

So is it simply taking the bitwise OR of the two values or is there more to this than meets the eye? If it's just the bitwise OR, then in the above I would end up with

//think math, not java for syntax below
paramsWM.gravity = Gravity.TOP|Gravity.LEFT = 48|3=51;

Again, if it's just bitwise, this shortcut is only permissible when I know the recipient (i.e. paramsWM.gravity) can handle the addition: right? Furthermore, could I equally have written

//this is java again
final WindowManager.LayoutParams paramsWM
...
paramsWM.gravity = Gravity.TOP + Gravity.LEFT;
Katedral Pillon
  • 14,534
  • 25
  • 99
  • 199
  • 2
    `|` is also enabled for boolean expressions. In such cases it works like `||` but without short-cutting. – PM 77-1 Aug 01 '15 at 02:11

1 Answers1

0

The pipe character simply takes the bitwise OR, nothing more, nothing less.

Here's a nice example for all bitwise operators: http://www.tutorialspoint.com/java/java_bitwise_operators_examples.htm

EDIT: As stated by user PM 77-1 in the comments, it's also used in bool expressions if you don't want to short-circuit