What is the difference between Bitwise AND & and LOGICAL AND &&

22,937

Solution 1

& modifies integers with bitwise operations, ie. 1000 & 1001 = 1000, && compares boolean values. However, & doubles as the non-shortcircuiting logical and, meaning if you have false & true, the second parameter would still be evaluated. This won't be the case with &&.

Solution 2

Bitwise, as its name implies, it's an AND operation at the BIT level.

So, if you perform a BITWISE AND on two integers:

int a = 7;     // b00000111
int b = 3;     // b00000011
int c = a & b; // b00000011 (bitwise and)

On the other hand, in C#, logical AND operates at logical (boolean) level. So you need boolean values as operators, and result is another logical value:

bool a = true;
bool b = false;
bool c = a && b; // c is false
c = a && true; // c is true

But only at the logical level.

Solution 3

Well, Good question (duplicated though).

Bitwise AND will affect its operators on the bit-level i.e. looping and doing logical AND operation on every bit.

On the other hand,

Logical AND will take 2 boolean operators to check their rightness (as a whole) and decide upon (notice that bool in C# is 2 bytes long).

Share:
22,937
GibboK
Author by

GibboK

A professional and enthusiastic Senior Front End Developer. Listed as top 2 users by reputation in Czech Republic on Stack Overflow. Latest open source projects Animatelo - Porting to JavaScript Web Animations API of Animate.css (430+ stars on GitHub) Industrial UI - Simple, modular UI Components for Shop Floor Applications Frontend Boilerplate - An opinionated boilerplate which helps you build fast, robust, and adaptable single-page application in React Keyframes Tool - Command line tool which convert CSS Animations to JavaScript objects gibbok.coding📧gmail.com

Updated on July 20, 2022

Comments