CodeGym /Java Course /Java Multithreading /Bitwise operators (&, XOR, <<, ...)

Bitwise operators (&, XOR, <<, ...)

Java Multithreading
Level 10 , Lesson 7
Available
Bitwise operators (&, XOR, <<, ...) - 1

"Hi, Amigo!"

"One more little lesson about bitwise operators."

"You know that in addition to the logical operators AND (&&), OR (||) and NOT (!), there are also bitwise operators AND (&), OR (|), NOT (~), and XOR(^), right?"

"Yep. Bilaabo once gave a very good lesson about this."

"Well, about these operators. I've got two things to tell you:"

"First, except for NOT (~), they can be applied to boolean variables, just like logical operators."

"Second, lazy evaluation does not apply to them."

"Look at this example:"

Code Equivalent code
if (a != null && a.getName() != null && c != null)
{
 c.setName(a.getName());
}
if (a != null)
{
 if (a.getName() != null)
 {
  if (c != null)
  {
   c.setName(a.getName());
  }
 }
}

"Is the left side more compact than the right?"

"Yep."

"And does it have the same meaning?"

"Yep."

"Quite right. But now look at the same expression using bitwise operators:"

Code Equivalent code
if (a != null & a.getName() != null & c != null)
{
 c.setName(a.getName());
}
boolean c1 = (a != null);
boolean c2 = (a.getName() != null);
boolean c3 = (c != null);
if (c1)
{
 if (c2)
 {
  if (c3)
  {
   c.setName(a.getName());
 }
 }
}

"In other words, the code is the same, but absolutely every operation will be performed."

"Note that if a is null, an exception will be thrown when calculating c2!"

"Ah. I can see that more clearly now."

Comments (5)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Niko Level 41, Wuhan, China
3 April 2022
位运算可用于布尔值的逻辑运算
TheLordJackMC Level 39, Princeton, idk somewhere
5 August 2021
gopros and bitwise operators are essential to the toolkit of a competent programmer
BlueJavaBanana Level 37
4 December 2020
I'm a bit confused here. The second example looks like the logic is ideantical apart from that if a is null we get an exception, whereas we don't in the first example.
Chang You Level 47, Santa Rosa, United States
27 December 2020
I think the point is "every operation will(still) be performed." In the bitwise case , The first three lines:

boolean c1 = (a != null);
boolean c2 = (a.getName() != null);
boolean c3 = (c != null);
That means we performed all the three operations and assign them to c1, c2, c3 before evaluated based on bitwise operator, regardless there's an exception or not. Whereas in the logical operator case, once a = null, the rest

a.getName() != null
c != null
won't be performed.
BlueJavaBanana Level 37
27 December 2020
Thank you Chang, I understand now. In the second code snippet the assignment occours as does the expression evaluation inside each (). This occours no matter what; Lazy evaluation does not matter with bitwise operators. Everything is always evaluated. The first example however uses logical operators and lazy evaluation is applicable here. So if the first expression evaluates to false, no futher evaluation takes place. Thanks for the heads up! Id also be greatful if you could tell me your next steps now you have reached level 41. What are your plans and aims?