when and why use or equalto (|=) shorthand operator - java

I was looking source code of a java project, herein i found an operator |= for boolean variables.
Can anybody tell me what exactly is this? and best way to use this.
Thanks for reply, now improving my question: what |= shorthand operator actually does. as per my test it shows:
false | true = true
false | false = false
true | false = true
true | true = true
But i still not clear, how it determines the result. And any use case where i can use this.
Thanks

a |= b; means the same as a = a | b;, in the same way that a += b; means the same as a = a + b;.
You would use it whenever you have something of the form a = a | b; (which is rare) and want to shorten it.

As you have got your answer that it is a short and a compound assignment operator. So if you write
a1 |= a2;
or
a1 = a1 | a2;
the both mean the same thing. Its just the way to write the code.
Regarding the two W's which you have asked i.e, when to use and why to use? is completely dependent upon the programmer as some programmer finds the first one as more readable and some find the latter.
Here is a list of other such operators.
Operator Description Example
----------------------------------------------------------------------------------------
|= bitwise inclusive OR and assignment operator C |= 2 is same as C = C | 2
^= bitwise exclusive OR and assignment operator C ^= 2 is same as C = C ^ 2
&= Bitwise AND assignment operator C &= 2 is same as C = C & 2
<<= Left shift AND assignment operator C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2
----------------------------------------------------------------------------------------

It's a short form for an assignment doing a logic OR with the left-hand operand.
a|=b ---> a=a|b
You can do the same thing with other operators: +, -, *, &, ^ , etc.
Update:
Adding something because it seems you need a short explanation of the logical or: This operator returns true only if at least one of its boolean operands is true. See this page on wiki.

Related

Java - The difference between || and | [duplicate]

This question already has answers here:
Why do we usually use || over |? What is the difference?
(28 answers)
Closed 5 years ago.
A book I'm working from states :
The short-circuit operators are nearly identical to the logical operators,
& and |, respectively, except that the right-hand side of the expression may never be evaluated if the final result can be determined by the left-hand side of the expression
To test this out I tried
int y = 1;
boolean x = false | (y < 4);
System.out.print(x); //true
Which printed out true as expected.
however
int y = 1;
boolean x = false || (y < 4);
System.out.print(x); //true
also prints out true which seems to contradict the book "the right-hand side of the expression may never be evaluated if the final result can be determined by the left-hand side of the expression".
I was assuming x would take the value of false as the final result can be determined from the left hand side alone
I was assuming x would take the value of false as the final result can be determined from the left hand side alone
No, the final result cannot be determined by the left hand side alone - since the left operand is false, the right operand must be evaluated too (since false OR something == something).
On the other hand, if the left operand was true, the result of the OR operator would be true, and the right operand could be ignored.
You should understand that short-circuit operators should give the same result as their non-short circuited counterparts (except for cases where evaluating the right operand may throw an exception and the short-circuited operators can determine the result by the left operand alone).
There would be no scenarios in which the short circuited operator returns false and the corresponding non-short circuited operator returns true or vice versa.

Java syntax - extra plus sign after cast is valid?

So I came across something that confused me when casting a byte to char, usually I would do this:
for (byte b:"ABCDE".getBytes()) {
System.out.println((char)b);
}
Which will print out
A
B
C
D
E
I accidentally left a + between the (char) and b and got the same result!?
Like so:
for (byte b:"ABCDE".getBytes()) {
System.out.println((char) + b);
}
Why exactly is this happening?
Am I essentially doing (char)(0x00 + b)? Because
System.out.println((char) - b);
yields a different result.
Note: Using Java version 1.8.0_20
Why exactly is this happening?
If you put a unary - operator before a number or expression it negates it.
Similarly, if you put a unary + operator before a number or expression it does nothing.
A safer way to convert a byte to a char is
char ch = (char)(b & 0xFF);
This will work for characters between 0 and 255, rather than 0 to 127.
BTW you can use unary operators to write some confusing code like
int i = (int) + (long) - (char) + (byte) 1; // i = -1;
b is a byte, and that be expressed as + b as well. For example, 3 can be written as +3 as well. So, ((char) + b) is same as ((char) b)
The + is the unary plus operator - like you can say that 1 is equivalent to +1, b is equivalent to +b. The space between + and b is inconsequential. This operator has a higher precedence than the cast, so after it's applied (doing nothing, as noted), the resulting byte is then cast to a char and produces the same result as before.

what does " |= "and " ^= " mean in java? [duplicate]

This question already has answers here:
What does this sign exactly mean? |=
(5 answers)
Closed 8 years ago.
this is a function for changing bit value of image. what does |= and ^= mean?
private int setBitValue(int n,int location,int bit) {
int toggle=(int)Math.pow(2,location),bv=getBitValue(n,location);
if(bv==bit)
return n;
if(bv==0 && bit==1)
n|=toggle; // what does it do?
else if(bv==1 && bit==0)
n^=toggle; // what does it do?
return n;
}
Its the same short form as in +=
n |= toogle
is the same as
n = n | toogle
the | is here the binary-or operator
and ^ is the binary xor-operator
They are short hand assignment operations.
n|=toggle; is equivalent to n=n|toggle;
and
n^=toggle; is equivalent to n=n^toggle;
And
| is bitwise OR
^ is bitwise XOR
They're the bitwise OR equals and the bitwise XOR equals operators. They are mainly used for dealing with bit flags. I highly recommend this article if you want to learn more about bitwise and bit-shifting operations.
These are shorthand bitwise operators. Like += using |= is the same as:
a = a | b;
Read the Oracle Documentation about Bitwise and Bit Shift Operators for further information.

What does result = result + (char)(c ^ d) do? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
^ operator in java
I was assuming that c ^ d is a calculation like 'the power of', so c = 5, d = 2, result is 25. I think I'm wrong, though.
Can you explain what (c ^ d) does in java, for example in
result = result + (char)(c ^ d)
^ is the bitwise xor meaning that 0b0101^0b0010 (5^2) is 0b0111 and 0b0101^0b0111 is 0b0010
look at the truth table of xor (the result is 1 if the input are different
a b | a^b
---------
0 0 | 0
0 1 | 1
1 0 | 1
1 1 | 0
the bitwise operators take each bit of the terms and apply the operator to each bit
The ^ operator performs a Bitwise Exclusive OR.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
For raising a number to a power, you would use the Math.pow function.
That one is the bitwise XOR operator.
^ is a bitwise XOR.

What is the difference between += and =+?

What is the difference between += and =+?
Specifically, in java, but in general also.
i += 4;
means
i = i + 4; // increase i by 4.
While
i =+ 4;
is equivalent to
i = +4; // assign 4 to i. the unary plus is effectively no-op.
(See http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.15.3 for what a unary + does.)
+= is an operator that increments the left-hand side of the assignment by the value of the right-hand side and assigns it back to the variable on the left-hand side. =+ is not an operator but, in fact, two operators: the assignment operator = and the unary plus + (positive) operator which denotes the value on the right-hand side is positive. It's actually redundant because values are positive unless they are negated with unary minus. You should avoid the =+ construct as it's more likely to cause confusion than do any actual good.
+= is get and increment:
a += 5; // adds 5 to the value of a
=+ isn't really a valid identifier on its own, but might show up when you're using the unary + operator:
a =+ 5; // assigns positive five to a
=+ is not an operator. The + is part of the number following the assignment operator.
int a = 4;
int b = 4;
a += 1;
b =+1;
System.out.println("a=" + a + ", b=" + b);
This shows how important it is to properly format your code to show intent.
+= is a way to increment numbers or String in java. E.g.
int i = 17;
i += 10; // i becomes 27 now.
There is no =+ operator. But if you do i =+ 10; it means i is equal to +10 which is equal to just 10.
Specifically, in java, but in general also.
In Java x += <expr>; is equivalent to x = x + ( <expr> ); where the + operator may be the arithmetical add operator or the string concatenation operator, depending on the type of x. On the other hand, x =+ <expr>; is really an ugly way of writing x = + <expr>; where the + is unary plus operator ... i.e. a no-op for numeric types and a compilation error otherwise.
The question is not answerable in the general case. Some languages support a "+=" operator, and others don't. Similarly, some languages might support a "=+" operator and others won't. And some languages may allow an application to "overload" one or other of the operators. It simply makes no sense to ask what an operator means "in general".
I don't know what you mean by "in general", but in the early versions of C language (which is where most of Java syntax came from, through C++), =+ was the original syntax for what later became +=, i.e. i =+ 4 was equivalent to i = i + 4.
CRM (C Reference Manual) is the document the describes C language with =+, =-, =>> and so on.
When you have a+=b, that means you're adding b to whatever's already in a. If you're doing a=+b, however, you're assigning +b to a.
int a=2;
int b=5;
a+=b;
System.out.println(a); //Prints 7
a=2;
b=5;
a=+b;
System.out.println(a); //Prints 5
The += operation as you said, is used for increment by a specific value stated in the R value.Like,
i = i+1;
//is equivalent to
i += 1;
Whereas, =+ is not any proper operation, its basically 2 different operators equal and unary plus operators written with each other.Infact the + sign after = makes no sense, so try not to use it.It will only result in a hocum.
i =+ 1;
//is equivalent to
i = +(1);

Categories