Difference between & and && ,| and ||

Hello techno thirsty guys, there is a simple logic behind the & and && , | and ||. Single & and | are the Bit-wise operators while && and || are the Logical operators, which distinguish both of them from each other.Let us look into the examples.

 Difference between & and &&

Below is the example. Please look into that

int count = 0;

if (count != 0 && 12 / count == 0)
{
    //come inside
}

While we execute above code, we would get below execution result:
bitwise vs logical operators-1

As per shown above example you have noticed that second condition would never come in execution.  Which says that if first condition is false, compiler will not check for next condition in && case.

Now we will try to understand the logic with the single & with example.

int count = 0;

if (count != 0 & 12 / count == 0)
{
    //come inside
}

While we execute above code, we would get below execution result:
bitwise vs logical operators-1
As per shown above example you have noticed that second condition has been executed and gives us error as Attempted to divide by zero. Which says that whether first condition was false or true, compiler will always check for next condition in & case.

Same way | and || are working.

Hope you have enjoyed this post. Please give me a feedback to us, so we could improve our blog quality.

 

Folks you should also refer below articles for better understanding of differences between confusing concepts:



Leave a Reply