Open In App

# What are the differences between bitwise and logical AND operators in C/C++?

A Bitwise And operator is represented as ‘&’ and a logical operator is represented as ‘&&’. The following are some basic differences between the two operators.

a) The logical and operator ‘&&’ expects its operands to be boolean expressions (either 1 or 0) and returns a boolean value.
The bitwise and operator ‘&’ work on Integral (short, int, unsigned, char, bool, unsigned char, long) values and return Integral value.

## C++

 `#include``using` `namespace` `std;``int` `main()``{``    ``int` `x = 3;  ``//...0011``    ``int` `y = 7;  ``//...0111` `    ``// A typical use of '&&'``    ``if` `(y > 1 && y > x)``      ``cout<<``"y is greater than 1 AND x\n"``;` `    ``// A typical use of '&'``    ``int` `z = x & y;   ``// 0011``   ` `    ``cout<<``"z = "``<< z;` `    ``return` `0;``}` `// this code is contributed by shivanisinghss2110`

## C

 `#include``int` `main()``{``    ``int` `x = 3;  ``//...0011``    ``int` `y = 7;  ``//...0111` `    ``// A typical use of '&&'``    ``if` `(y > 1 && y > x)``      ``printf``(``"y is greater than 1 AND x\n"``);` `    ``// A typical use of '&'``    ``int` `z = x & y;   ``// 0011``   ` `    ``printf` `(``"z = %d"``, z);` `    ``return` `0;``}`

Output

```y is greater than 1 AND x
z = 3```

Time Complexity: O(1)

Auxiliary Space: O(1)

b) If an integral value is used as an operand for ‘&&’ which is supposed to work on boolean values, the following rule is used in C.
…..Zero is considered as false and non-zero is considered as true.

For example in the following program x and y are considered as 1.

## C++

 `#include ``using` `namespace` `std;` `// Example that uses non-boolean expression as``// operand for '&&'``int` `main()``{``    ``int` `x = 2, y = 5;``    ``int` `z = x && y;``    ``cout << ``" "` `<< z;``    ``return` `0;``}` `// this code is contributed by shivanisinghss2110`

## C

 `#include``// Example that uses non-boolean expression as``// operand for '&&'``int` `main()``{``   ``int` `x = 2, y = 5;``   ``printf``(``"%d"``, x&&y);``   ``return` `0;``}`

Output

` 1`

Time Complexity: O(1)

Auxiliary Space: O(1)

It is compiler error to use the non-integral expression as operand for bitwise &. For example the following program shows compiler error.

## C++

 `#include``using` `namespace` `std;``// Example that uses non-integral expression as``// operator for '&'``int` `main()``{``   ``float` `x = 2.0, y = 5.0;``   ``cout <<``" "``<< x&y;``   ``return` `0;``}` `// this code is contributed by shivanisinghss2110`

## C

 `#include``// Example that uses non-integral expression as``// operator for '&'``int` `main()``{``   ``float` `x = 2.0, y = 5.0;``   ``printf``(``"%d"``, x&y);``   ``return` `0;``}`

Output:

`error: invalid operands to binary & (have 'float' and 'float')`

Time Complexity: O(1)

Auxiliary Space: O(1)

c) The ‘&&’ operator doesn’t evaluate the second operand if the first operand becomes false. Similarly ‘||’ doesn’t evaluate the second operand when first operand becomes true. The bitwise ‘&’ and ‘|’ operators always evaluate their operands.

## C++

 `#include ``using` `namespace` `std;``int` `main()``{``    ``int` `x = 0;` `    ``// 'Geeks in &&' is NOT``    ``// printed because x is 0``    ``printf``(``"%d\n"``, (x && ``printf``(``"Geeks in && "``)));` `    ``// 'Geeks in &' is  printed``    ``printf``(``"%d\n"``, (x & ``printf``(``"Geeks in & "``)));` `    ``return` `0;``}``//this code is contributed by aditya942003patil`

## C

 `#include``int` `main()``{``    ``int` `x = 0;` `    ``// 'Geeks in &&' is NOT``    ``// printed because x is 0``    ``printf``(``"%d\n"``, (x && ``printf``(``"Geeks in && "``)));` `    ``// 'Geeks in &' is  printed``    ``printf``(``"%d\n"``, (x & ``printf``(``"Geeks in & "``)));` `    ``return` `0;``}`

Output

```0
Geeks in & 0```

Time Complexity: O(1)

Auxiliary Space: O(1)

The same differences are there between logical OR ‘||’ and bitwise OR ‘|’.