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

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.

Related

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)

How does the implementation of this bitwise operator make sense?

In an earlier question regarding how to maximize the JFrame I saw this bit of code and it worked. I took out the
name.getExtendedState()
and it still worked? What does the use of the "getter" and the OR symbol accomplish?
name.setExtendedState(name.getExtendedState()|JFrame.MAXIMIZED_BOTH);
Using name.getExtendedState()|JFrame.MAXIMIZED_BOTH means that you're adding MAXIMIZED_BOTH to the existing extended state. If you say only JFrame.MAXIMIZED_BOTH, that means you're replacing the extended state with only that bit, and throwing away anything in the current extended state.
From the API getExtendedState() :
Gets the state of this frame. The state is represented as a bitwise mask.
NORMAL
Indicates that no state bits are set.
ICONIFIED
MAXIMIZED_HORIZ
MAXIMIZED_VERT
MAXIMIZED_BOTH
Concatenates MAXIMIZED_HORIZ and MAXIMIZED_VERT.
The logical OR will combine the returned value with the value of JFrame.MAXIMIZED_BOTH
for example if NORMAL was 10110 and MAXIMIZED_BOTH was 01100, ORing the two would yeild 1110
Normal 10110
MaxBoth 01100
Result 11110
Quoted from Wikipedia: http://en.wikipedia.org/wiki/Bitwise_operation#OR
A bitwise OR takes two bit patterns of equal length and performs the
logical inclusive OR operation on each pair of corresponding bits. The
result in each position is 1 if the first bit is 1 or the second bit
is 1 or both bits are 1; otherwise, the result is 0. For example:
0101 (decimal 5)
OR 0011 (decimal 3)
= 0111 (decimal 7)
So if getExtendedState() is returning a number made up of binary flags (ie. a bit field)... ORing it (using the pipe operator | ), is simply keeping ALL the existing flags in the object's state and also setting the bit/s that correspond to the state JFrame.MAXIMIZED_BOTH.
This is because ORing sets a bit to 1 if it is 1 in either the first operand OR the second operand.
Hope that helps explain it.

Understanding signed numbers and complements in java

I have a 3 byte signed number that I need to determine the value of in Java. I believe it is signed with one's complement but I'm not 100% sure (I haven't studied this stuff in more than 10 years and the documentation of my problem isn't super clear). I think the problem I'm having is Java does everything in two's complement. I have a specific example to show:
The original 3-byte number: 0xEE1B17
Parsed as an integer (Integer.parseInt(s, 16)) this becomes: 15604503
If I do a simple bit flip (~) of this I get (I think) a two's complement representation: -15604504
But the value I should be getting is: -1172713
What I think is happening is I'm getting the two's complement of the entire int and not just the 3 bytes of the int, but I don't know how to fix this.
What I have been able to do is convert the integer to a binary string (Integer.toBinaryString()) and then manually "flip" all of the 0s to 1s and vice-versa. When then parsing this integer (Integer.parseInt(s, 16)) I get 1172712 which is very close. In all of the other examples I need to always add 1 to the result to get the answer.
Can anyone diagnose what type of signed number encoding is being used here and if there is a solution other than manually flipping every character of a string? I feel like there must be a much more elegant way to do this.
EDIT: All of the responders have helped in different ways, but my general question was how to flip a 3-byte number and #louis-wasserman answered this and answered first so I'm marking him as the solution. Thanks to everyone for the help!
If you want to flip the low three bytes of a Java int, then you just do ^ 0x00FFFFFF.
0xFFEE1B17 is -1172713
You must only add the leading byte. FF if the highest bit of the 3-byte value is set and 00 otherwise.
A method which converts your 3-byte value to a proper intcould look like this:
if(byte3val>7FFFFF)
return byte3val| 0xFF000000;
else
return byte3val;
Negative signed numbers are defined so that a + (-a) = 0. So it means that all bits are flipped and then 1 added. See Two's complement. You can check that the condition is satisfied by this process by thinking what happens when you add a + ~a + 1.
You can recognize that a number is negative by its most significant bit. So if you need to convert a signed 3-byte number into a 4-byte number, you can do it by checking the bit and if it's set, set also the bits of the fourth byte:
if ((a & 0x800000) != 0)
a = a | 0xff000000;
You can do it also in a single expression, which will most likely perform better, because there is no branching in the computation (branching doesn't play well with pipelining in current CPUs):
a = (0xfffffe << a) >> a;
Here << and >> perform byte shifts. First we shift the number 8 bits to the right (so now it occupies the 3 "upper" bytes instead of the 3 "lower" ones), and then shift it back. The trick is that >> is so-called Arithmetic shift also known as signed shift. copies the most significant bit to all bits that are made vacant by the operation. This is exactly to keep the sign of the number. Indeed:
(0x1ffffe << 8) >> 8 -> 2097150
(0xfffffe << 8) >> 8 -> -2
Just note that java also has a unsigned right shift operator >>>. For more information, see Java Tutorial: Bitwise and Bit Shift Operators.

How to reverse this bit operand?

Basically I have a number that is manipulated with bitwise operands as you can see here:
is[i_6_] = i_9_ - 256 | ~0x7fffffff;
I need to reverse/undo this bit operand..
| ~0x7fffffff
So that I would just have the value of:
i_9_ - 256r help!
How to reverse this bit operand?
1st of all im assumming the expression you wrote is actually (i_9_ - 256) | ~0x7fffffff
| ~0x7fffffff
is effectively the same as
| 0x80000000
meaning it will make the highest bit of the original number (i_9_ - 256 according to what you said) into a "1" regardless of its original value.
so i dont think you can reverse this
To just remove that bit would be, as you likely know:
& 0x7fffffff
But since you need to revert it to its previous state, you would need to--generally speaking--save the state of that most-significant bit prior to OR'ing to determine whether you should clear that bit or not. The most direct way to obtain your result, then, would be to re-use the original value of i_9_, but without the OR operation this time:
= i_9_ - 256
Using this: And with the (~) inverse of the value
i_9_ - 256 & 0x7fffffff;
inverse of inverse ~(~)

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

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...

Categories

Resources