Use the or “|” operator on statements in Java
Suppose I have an array a [], if I want to change the value of a [i] and change it to zero, I can do it by using the temp variable
int temp = a[i]; a[i] = 0;
But I encountered similar code
int temp = a[i] | (a[i] = 0);
It's hard for me to understand that Please explain whether it works? Is it a good habit to use similar types of code?
Solution
The purpose of the direct code is to get the value from the array and set its position in the array to 0
Let's see how tricky code does it
|The operator is a bitwise operator First, evaluate a [i], regardless of the value of the operand on the left Next, brackets force a [i] = 0 for evaluation This sets the array element to 0 and the right operand of | to now 0 Executing bitwise or a value of 0 does not change other values Temp = the value of the entire expression on the right is the original value of a [i] In one statement, this has the effect of doing everything simple code does
This code is tricky. It's not a good habit because it's confusing I will never use this technology