# 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 ‘&&’. 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 ‘&’ works on Integral (short, int, unsigned, char, bool, unsigned char, long) values and return Integral value.

`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 y\n"` `); ` ` ` ` ` `// A typical use of '&' ` ` ` `int` `z = x & y; ` `// 0011 ` ` ` ` ` `printf` `(` `"z = %d"` `, z); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output

y is greater than 1 AND y z = 3.

**b)** If an integral value is used as an operand for ‘&&’ which is supposed to work on boolean values, following rule is used in C.

…..A 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.

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

*chevron_right*

*filter_none*

Output

1

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

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

*chevron_right*

*filter_none*

Output:

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

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

`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; ` `}` |

*chevron_right*

*filter_none*

Output:

0 Geeks in & 0

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

This article is contributed by** Ujjwal Jain**. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Operators in C | Set 2 (Relational and Logical Operators)
- Order of operands for logical operators
- Bitwise Operators in C/C++
- Bitwise right shift operators in Java
- Toggle case of a string using Bitwise Operators
- Check if a number is divisible by 8 using bitwise operators
- Check if a number is divisible by 17 using bitwise operators
- Check if a number is multiple of 9 using bitwise operators
- Russian Peasant (Multiply two numbers using bitwise operators)
- Case conversion (Lower to Upper and Vice Versa) of a string using BitWise operators in C/C++
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Code Optimization Technique (logical AND and logical OR)
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Operators in C | Set 1 (Arithmetic Operators)