0

In logic expressions we use two " and(&) " operands what is the reason ? How does it work at the background of program? I mean how does machine run that instruction ?

Trinity
  • 486
  • 8
  • 27
  • 1
    The choice of `&&` vs `&` is a **language-specific syntax decision** and is largely for 'historical reasons' at this point, where it even applies. Likewise, the exact rules (eg. short-circuiting or not?) and implementation are language/run-time specific. – user2864740 Apr 20 '16 at 04:22
  • Please follow the thread : http://stackoverflow.com/questions/4163483/what-is-the-diffrence-between-and-operators-in-c-sharp – Ranju Jacob Apr 20 '16 at 04:26

2 Answers2

1

& is a bitwise operator and it always evaluates both sides.

&& is a logical operator, so it evaluates the left side and if it's true then it proceeds to evaluate the right side. That's why it's sometimes called a short-circuit operation using &&.

Keep in mind, this is how the mentioned operators are implemented in a vast number of languages(C,C#,Java etc) and you should check the documentation for the specific language that you are working with.

just_a_coder
  • 282
  • 1
  • 4
  • 14
0

Typically having two "and" operands is to differentiate between logical and and bitwise and.

What is the difference between these operations?

"Logical and" (usually represented as && or "and" in most languages) compares two boolean values. The important thing here is that a "logical and" performs operations on values that are either true or false. The underlying implementation of how true and false are represented in terms of bits really doesn't matter.

"Bitwise and" (represented by a single "&") performs a "logical and" between two values for every bit in those values. Take for example two 8 bit unsigned integers: 5 and 9. In binary, 5 looks like 00000101. 9 looks like 00001001. A "bitwise and" calculates the bits that are set to 1 in both numbers. The result of a bitwise and between 5 and 9 would be 1, which is 00000001 in binary.

Googling "bitwise operations" will give you more information on this.