Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java & operator with two integers?

From what I understand the & operator is similar to the && operator except that the && only checks the second if the first is true, while the & checks both regardless of the result of the first one. Basically the && just saves a little time and power.

If that is so, then how does this code work?

int l = 0;
if ((l & 8) != 0 && (l & 4) == 0){ do something}

what does the (l & 8) and the (l & 4) do? What does the & do in this case?

like image 646
nife552 Avatar asked Dec 17 '13 23:12

nife552


People also ask

What is Java used for?

One of the most widely used programming languages, Java is used as the server-side language for most back-end development projects, including those involving big data and Android development. Java is also commonly used for desktop computing, other mobile computing, games, and numerical computing.

What exactly is Java?

Java is a programming language and computing platform first released by Sun Microsystems in 1995. It has evolved from humble beginnings to power a large share of today's digital world, by providing the reliable platform upon which many services and applications are built.

Is Java a programming or coding?

Java is a popular programming language, created in 1995. It is owned by Oracle, and more than 3 billion devices run Java. It is used for: Mobile applications (specially Android apps)

What is Java called now?

Java, also spelled Djawa or Jawa, island of Indonesia lying southeast of Malaysia and Sumatra, south of Borneo (Kalimantan), and west of Bali. Java is home to roughly half of Indonesia's population and dominates the country politically and economically.


1 Answers

& and && are two different operators but the difference is not what you've described.

& does the bit-wise AND of two integers and produces a third integer whose bit are set to 1 if both corresponding bits in the two source integers are both set to 1; 0 otherwise.

&& applies only to two booleans and returns a third boolean which will be true if both the input booleans are true; false otherwise.

like image 51
Raffaele Rossi Avatar answered Oct 14 '22 09:10

Raffaele Rossi