What vertical bar('|') means in android? - java

For example:
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK|
PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.ON_AFTER_RELEASE, "Alarm");
What does the ' | ' character mean?
More details about the problem:
I'm asking this because when I instantiate the wakelock with only PowerManager.AQUIRE_CAUSES_WAKEUP the program stops working, where as when I use the way above, it works fine.
I'm wondering if the cause of this is because the program ignore the ACQUIRE_CAUSES_WAKEUP tag and it ends up not being used.

The | is a bitwise or, and it goes beyond Android. It is often used to stuff multiple options into one parameter.
So a function of the form f(X|Y|Z) means the function should use options X, Y and Z. Of course, X, Y and Z should be appropriately coded to ensure | will preserve their values.

From the doc
bitwise inclusive OR => |
The Bitwise inclusive OR ( | ) operator performs the bitwise inclusive OR operation on each parallel pair of bits of two operands. In each pair, the result is 1, if either first or second bit is 1 (or both are 1). Otherwise the result is 0. Lets see the table of using inclusive operations.
Lets understand the inclusive OR operations using truth table:
(OR)
A B Result
0 0 0
1 0 1
0 1 1
1 1 1

If you look at constants most often used with | in the type of example you've shown, their values are powers of 2. For example:
Options.OPTION1 = 1;
Options.OPTION2 = 2;
Options.OPTION3 = 4;
Options.OPTION4 = 8;
In binary (Note I have omitted the 0b prefix for ease of reading):
Options.OPTION1 = 0001;
Options.OPTION2 = 0010;
Options.OPTION3 = 0100;
Options.OPTION4 = 1000;
If you OR Options.OPTION1 and Options.OPTION3, the result is 0101;
Options.OPTION1 | Options.OPTION3 => 0101
This enables you to pack multiple values into one since each combination of options is unique.
You can "extract" the options from the packed value by ANDing the options:
packedValue = Options.OPTION1 | Options.OPTION3;
packedValue & Options.OPTION3 => true;
packedValue & Options.OPTION4 => false;
Since
0101 AND 0100 => 0100 => true
and
0101 AND 1000 => 0000 => false

In general, that symbol (|) is a bitwise OR. It's used in a lot of different languages and environments outside of Android.
It's usage is:
"X|Y : if X or Y is 1, then the result is 1"
In your specific case it's being used to create a bit field. Besure to review the Android power manager code base here.
Possible flags for this API are:
PARTIAL_WAKE_LOCK = 0x01
SCREEN_DIM_WAKE_LOCK = 0x06
SCREEN_BRIGHT_WAKE_LOCK = 0x0a
FULL_WAKE_LOCK = 0x1a
These are mutually exclusive (you can only pick one), but you can "OR in" some other flags:
ON_AFTER_RELEASE = 0x20000000
ACQUIRE_CAUSES_WAKEUP = 0x10000000
So once your code runs it ORs these flags together resulting in:
0x20000000
| 0x10000000
| 0x0000001a
---------------
0x3000001a
I'm asking this because when i instantiate the wakelock with only "PowerManager.AQUIRE_CAUSES_WAKEUP" the program stops working
That's because you have to pick one of the levels of wake lock (PARTIAL, SCREEN_DIM, SCREEN_BRIGHT, or FULL), you're trying to run with just one of the optional wake lock flags...

Related

How do I represent potency in Java with all zeros? [duplicate]

What function does the ^ (caret) operator serve in Java?
When I try this:
int a = 5^n;
...it gives me:
for n = 5, returns 0
for n = 4, returns 1
for n = 6, returns 3
...so I guess it doesn't perform exponentiation. But what is it then?
The ^ operator in Java
^ in Java is the exclusive-or ("xor") operator.
Let's take 5^6 as example:
(decimal) (binary)
5 = 101
6 = 110
------------------ xor
3 = 011
This the truth table for bitwise (JLS 15.22.1) and logical (JLS 15.22.2) xor:
^ | 0 1 ^ | F T
--+----- --+-----
0 | 0 1 F | F T
1 | 1 0 T | T F
More simply, you can also think of xor as "this or that, but not both!".
See also
Wikipedia: exclusive-or
Exponentiation in Java
As for integer exponentiation, unfortunately Java does not have such an operator. You can use double Math.pow(double, double) (casting the result to int if necessary).
You can also use the traditional bit-shifting trick to compute some powers of two. That is, (1L << k) is two to the k-th power for k=0..63.
See also
Wikipedia: Arithmetic shift
Merge note: this answer was merged from another question where the intention was to use exponentiation to convert a string "8675309" to int without using Integer.parseInt as a programming exercise (^ denotes exponentiation from now on). The OP's intention was to compute 8*10^6 + 6*10^5 + 7*10^4 + 5*10^3 + 3*10^2 + 0*10^1 + 9*10^0 = 8675309; the next part of this answer addresses that exponentiation is not necessary for this task.
Horner's scheme
Addressing your specific need, you actually don't need to compute various powers of 10. You can use what is called the Horner's scheme, which is not only simple but also efficient.
Since you're doing this as a personal exercise, I won't give the Java code, but here's the main idea:
8675309 = 8*10^6 + 6*10^5 + 7*10^4 + 5*10^3 + 3*10^2 + 0*10^1 + 9*10^0
= (((((8*10 + 6)*10 + 7)*10 + 5)*10 + 3)*10 + 0)*10 + 9
It may look complicated at first, but it really isn't. You basically read the digits left to right, and you multiply your result so far by 10 before adding the next digit.
In table form:
step result digit result*10+digit
1 init=0 8 8
2 8 6 86
3 86 7 867
4 867 5 8675
5 8675 3 86753
6 86753 0 867530
7 867530 9 8675309=final
As many people have already pointed out, it's the XOR operator. Many people have also already pointed out that if you want exponentiation then you need to use Math.pow.
But I think it's also useful to note that ^ is just one of a family of operators that are collectively known as bitwise operators:
Operator Name Example Result Description
a & b and 3 & 5 1 1 if both bits are 1.
a | b or 3 | 5 7 1 if either bit is 1.
a ^ b xor 3 ^ 5 6 1 if both bits are different.
~a not ~3 -4 Inverts the bits.
n << p left shift 3 << 2 12 Shifts the bits of n left p positions. Zero bits are shifted into the low-order positions.
n >> p right shift 5 >> 2 1 Shifts the bits of n right p positions. If n is a 2's complement signed number, the sign bit is shifted into the high-order positions.
n >>> p right shift -4 >>> 28 15 Shifts the bits of n right p positions. Zeros are shifted into the high-order positions.
From here.
These operators can come in handy when you need to read and write to integers where the individual bits should be interpreted as flags, or when a specific range of bits in an integer have a special meaning and you want to extract only those. You can do a lot of every day programming without ever needing to use these operators, but if you ever have to work with data at the bit level, a good knowledge of these operators is invaluable.
It's bitwise XOR, Java does not have an exponentiation operator, you would have to use Math.pow() instead.
XOR operator rule =>
0 ^ 0 = 0
1 ^ 1 = 0
0 ^ 1 = 1
1 ^ 0 = 1
Binary representation of 4, 5 and 6 :
4 = 1 0 0
5 = 1 0 1
6 = 1 1 0
now, perform XOR operation on 5 and 4:
5 ^ 4 => 1 0 1 (5)
1 0 0 (4)
----------
0 0 1 => 1
Similarly,
5 ^ 5 => 1 0 1 (5)
1 0 1 (5)
------------
0 0 0 => (0)
5 ^ 6 => 1 0 1 (5)
1 1 0 (6)
-----------
0 1 1 => 3
It is the XOR bitwise operator.
Lot many people have already explained about what it is and how it can be used but apart from the obvious you can use this operator to do a lot of programming tricks like
XORing of all the elements in a boolean array would tell you if the array has odd number of true elements
If you have an array with all numbers repeating even number of times except one which repeats odd number of times you can find that by XORing all elements.
Swapping values without using temporary variable
Finding missing number in the range 1 to n
Basic validation of data sent over the network.
Lot many such tricks can be done using bit wise operators, interesting topic to explore.
XOR operator rule
0 ^ 0 = 0
1 ^ 1 = 0
0 ^ 1 = 1
1 ^ 0 = 1
Bitwise operator works on bits and performs bit-by-bit operation. Assume if a = 60 and b = 13; now in binary format they will be as follows −
a = 0011 1100
b = 0000 1101
a^b ==> 0011 1100 (a)
0000 1101 (b)
------------- XOR
0011 0001 => 49
(a ^ b) will give 49 which is 0011 0001
As others have said, it's bitwise XOR. If you want to raise a number to a given power, use Math.pow(a , b), where a is a number and b is the power.
AraK's link points to the definition of exclusive-or, which explains how this function works for two boolean values.
The missing piece of information is how this applies to two integers (or integer-type values). Bitwise exclusive-or is applied to pairs of corresponding binary digits in two numbers, and the results are re-assembled into an integer result.
To use your example:
The binary representation of 5 is 0101.
The binary representation of 4 is 0100.
A simple way to define bitwise XOR is to say the result has a 1 in every place where the two input numbers differ.
With 4 and 5, the only difference is in the last place; so
0101 ^ 0100 = 0001 (5 ^ 4 = 1) .
It is the Bitwise xor operator in java which results 1 for different value of bit (ie 1 ^ 0 = 1) and 0 for same value of bit (ie 0 ^ 0 = 0) when a number is written in binary form.
ex :-
To use your example:
The binary representation of 5 is 0101.
The binary representation of 4 is 0100.
A simple way to define Bitwise XOR is to say the result has a 1 in every place where the two input numbers differ.
0101 ^ 0100 = 0001 (5 ^ 4 = 1) .
To perform exponentiation, you can use Math.pow instead:
https://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Math.html#pow%28double,%20double%29
As already stated by the other answer(s), it's the "exclusive or" (XOR) operator. For more information on bit-operators in Java, see: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op3.html
That is because you are using the xor operator.
In java, or just about any other language, ^ is bitwise xor,
so of course,
10 ^ 1 = 11.
more info about bitwise operators
It's interesting how Java and C# don't have a power operator.
It is the bitwise xor operator in java which results 1 for different value (ie 1 ^ 0 = 1) and 0 for same value (ie 0 ^ 0 = 0).
^ is binary (as in base-2) xor, not exponentiation (which is not available as a Java operator). For exponentiation, see java.lang.Math.pow().
It is XOR operator. It is use to do bit operations on numbers. It has the behavior such that when you do a xor operation on same bits say 0 XOR 0 / 1 XOR 1 the result is 0. But if any of the bits is different then result is 1.
So when you did 5^3 then you can look at these numbers 5, 6 in their binary forms and thus the expression becomes (101) XOR (110) which gives the result (011) whose decimal representation is 3.
As an addition to the other answers, it's worth mentioning that the caret operator can also be used with boolean operands, and it returns true (if and only if) the operands are different:
System.out.println(true ^ true); // false
System.out.println(true ^ false); // true
System.out.println(false ^ false); // false
System.out.println(false ^ true); // true
^ = (bitwise XOR)
Description
Binary XOR Operator copies the bit if it is set in one operand but not both.
example
(A ^ B) will give 49 which is 0011 0001
In other languages like Python you can do 10**2=100, try it.

is there a straightforward way to turn off an integer bit in java?

im trying to simplify my database by saving boolean data in numeric fields, right now im doing this to turn on and off a bit.
int turnBit(int input, int bit, boolean value){
if(value){
input = input | (1<<bit);
}else{
input = input | (1<<bit);
input = input ^ (1<<bit);
}
return input;
}
is there any way to turn off the bit without turning it on before?
You are using the bitwise 'or' operator to turn on a bit, and this is correct:
0001 | 0010 results in 0011
You can use the bitwise 'and' function to turn off a bit:
1101 & 1110 results in 1100
So to turn off one bit of a value, you make a 'bit mask' which has all the bits you do NOT care about set to 1, and the one that you want to turn off set to 0; that will 'turn off' that bit in the value.
If you have a mask for turning on a particular bit -- that is, one with 0 in each position you don't want to change and a 1 in the position you do want to change, you can transform it into the "turn off" bit mask by applying java's bitwise 'not' operator (~), i.e., ~0010 results in 1101.
input = input & ~(1 << bit)

Is there any easy way to set several bits in a row within a short or int?

I am trying to find an easy way of setting several bits within an integer or a short value.
For a short value I want to set 5 bits, starting at bit number 6.
The value I want to set is a certain value I can store in a byte or short or whatever.
For the Java SetBits, I do not see anything where you can do something like this.
It seems like there should be an easy to do this.
Does anybody know of an easy way of doing this besides setting one bit at a time or something like that?
The simple way is to use the bitwise operations. For example, assuming the bits are numbered from the right starting with zero, this sets 5 bits to ones starting at offset 6.
int target = ...
target = target | 0x07b0; // that is 00000 0111 1100 0000
or if you are using Java 8:
target = target | 0b00000 0111 1100 0000;
If you want to set the same bits to value that you get from a variable, then ...
int target = ...
int value = ...
int temp = target & ( ~ 0b00000 0111 1100 0000 ); // clear current bit values
target = temp & ( value << 6 ); // set the new values.
Code involving low-level bit fiddling is messy and opaque, but the logic of the biwise operators (and the shift operators) is simple. If you are doing this in production code, then I strongly recommend that you define a bunch of constants with semantically relevant names and use them instead of integers.
I was hoping there was a builtin Java method ...
There isn't one, AFAIK. But you could easily code and test your own bit manipulation methods, if you think it would make your coding simpler. (The issue is that such an API probably would not improve code readability ... and the resulting code is likely to be harder for the JIT compiler to optimize.)
You can use binary literals in Java:
// An 8-bit 'byte' value:
byte aByte = (byte)0b00100001;
// A 16-bit 'short' value:
short aShort = (short)0b1010000101000101;
// Some 32-bit 'int' values:
int anInt1 = 0b10100001010001011010000101000101;
int anInt2 = 0b101;
int anInt3 = 0B101; // The B can be upper or lower case.
// A 64-bit 'long' value. Note the "L" suffix:
long aLong = 0b1010000101000101101000010100010110100001010001011010000101000101L;
More here

what does mean these two operator "|=" and "|"

I have found this line in an application source code but i cant figure out the meaning of the bitwise or inclusive operator "|" between the two flags.
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
I did not also understand the meaning of this operator |= in the following line:
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Someone could help me plz.
I'd started my answer while no-one else had answered, so I decided to finish it anyway...
The pipe and ampersand | and & perform the OR and AND operations respectively.
You'll be used to seeing || and &&, which perform the boolean logic OR and AND, and the use of a single | or & is a bitwise operation.
If you look on the flag documentation, the flag for clear_top is 0x04000000, and single_top is 0x20000000.
The operation which you are performing is therefore:
0x04000000 OR 0x20000000 = 0x24000000
Which sets the required bits in the intent to use both of the desired flags.
The a |= b operator is the overloaded equivalent of a = a | b, similar to the usage of +=, -- or ++, which you should be used to seeing elsewhere
a | b is bitwise OR of a and b.
Its the Assignment by bitwise OR
a1 |= a2;
is short for:
a1 = a1 | a2;
|= reads the same way as +=.
Let's for assume for example that FLAG_ACTIVITY_CLEAR_TOP is 2 and FLAG_ACTIVITY_SINGLE_TOP is 4.
So in binnary the represantion will be 0000000010 for the decimal value 2 and 00000100 for the value 4. The binary or operation between those two values will give the value 6 : 00000110 (The on bits on both 2 and 4 are on) . using power of two values for suck constants will make sure thatonly unique values will come out after th bitwise or:
For example : 1 is 00000001 2 is 00000010 4 is 00000100 8 is 00001000 16 is 00010000 .....
If you are settings flags this way- it's very easy to decode the original flags : just perform a bitwise AND operation with the original flag , if it's zero than the flag is not there - if it's the flag itself - then the flag is up.
For example : let's check 000011000 for the flag SOME_FLAG - and let's say for the porpuse of the example that it's value is 8 - 00001000. After the bitwise and operation : 00011000 & 00001000 - We will get 00001000 , ANDing with something else (that does not include the flag SOME_FLAG - like any other flag with a power of 2 value) will return 0.
It's the same as notification.flags = (notification.flags | Notification.FLAG_AUTO_CANCEL);
cf. a += b is equivalent to a = (a + b);
where I've used the superfluous parentheses for clarity.
As far as I know, these are bit operators.
As Bathsheba wrote, it's equal to (notification.flags | Notification.FLAG_AUTO_CANCEL);
It's an logical or, for informations see here: Oracle.com
Infos about or at Wikipedia.
If you look at those flags, you will see they are all powers of two. This means exactly one bit is set to 1, so performing a bitwise or in this case does just mean to set all those flags.

How does Java handle arguments separated by |?

How does Java handle arguments separated by | ?
for example
private void foo(int i) {
System.out.println(i);
}
private void bar() {
foo(1 | 2 | 1);
}
Which would give the output
3
I've seen this used in SWT/JFace widget constructors. What I can't figure out is how the value of i is decided.
The | is a bitwise or-operator.
foo(1 | 2 | 1);
means call foo with the argument 1 bitwise-or 2 bitwise-or 1.
1 in binary is 01
2 in binary is 10
Bitwise or of 01 and 10 is 11 which is 3 in decimal.
Note that the | operator can be used for booleans as well. Difference from the || operator being that the second operand is evaluated even if the first operand evaluates to true.
Actually, all bitwise operators work on booleans as well, including the xor ^. Here however, there are no corresponding logical operator. (It would be redundant, since there is no way of doing a "lazy" evaluation of ^ :)
it is using the bitwise OR operator. For starters, 1 | 1 = 1 so the second 1 is redundant. If we remove the redundant 1 we are left with the equation 1 | 2 = 3. Looking at it in 2 bit binary it looks like:
01 | 10 = 11
The or operator will match up the corresponding bits from each or the values and if there is one 1 in either or both values for a given position, the result is a 1. If both values for both the corresponding bits then the result is 0.

Categories

Resources