# Logical AND and bitwise AND dull mind

1. Aug 28, 2011

### rama1001

Logical AND and bitwise AND....dull mind

Hi,
I had read some descriptions about these on web but i fell, both doing same thing. More, i observe these are changing language to language. Can any tell me straight difference with example.. that would be helpful to me.

2. Aug 28, 2011

### MATLABdude

Re: Logical AND and bitwise AND....dull mind

Consider A, 0101, and B, 0110. The result of a logical AND of A and B would return TRUE, as you're ANDing two things that are non-zero. The result of a bitwise AND of A and B would return 0100. When you're dealing with booleans, or integers that are basically booleans, then yes, you'll have the same result.

Bitwise operators are really useful when you need to do bit-masking or low-level operations (or, so I'm told, to pass one of the "skill-testing questions" that HR departments love, but the solution for which has become so popularized via the interwebs that it's become banal).

If you restrict the use of logical operators to logical operations (when you have a few things that are either TRUE or FALSE, and you want an answer that's TRUE or FALSE), then you'll be in good stead.

EDIT: In short, bitwise: bit-by-bit. Logical: the whole thing.

3. Aug 28, 2011

### phinds

Re: Logical AND and bitwise AND....dull mind

The value of mathematical expression does not change "language to language", so what exactly do you mean when you say they are changing?

4. Aug 28, 2011

### rama1001

Re: Logical AND and bitwise AND....dull mind

If both results same, i mean logical and bitwise. How can i chose the valid one. Can you explain me bit clear wher we can use these two methods.

I saw on google that some asking the difference in C, C++, java..etc. So, i asked that language to language change.

5. Aug 28, 2011

### MarcoD

Re: Logical AND and bitwise AND....dull mind

The notions of the logical and bitwise AND occur in some languages, the most commonly known being the imperative C language. To be honest, most of the confusion stems from that these languages are not very well designed for humans but often inherit qualia from the underlying machine, more specific, the machine language they are translated to.

Having stated that, the notions of the logical and bitwise and are not very difficult to understand in a layman's manner.

The bitwise AND is an operator which takes two series of bits and evaluates both bit patterns by AND-ing the bits in their respective positions. E.g., 1101 AND 0110 becomes 0100, that value may latter have a logical meaning (TRUE or FALSE) in the given language.

The logical AND operates on two 'logical' values which may be understood to be individual bits. I.e., FALSE AND TRUE evaluates to FALSE. Moreover, procedures which evaluate to boolean values can be combined in this manner. E.g., f_returning_FALSE() AND g_returning_TRUE() is understood to denote the boolean value FALSE.

Because the logical AND is often used in conditional statements, usually the logical AND is evaluated in a left-to-right manner. E.g., in the example 'f_returning_FALSE() AND g_returning_TRUE()' only the first procedure call is made since that call will return FALSE, and therefor the whole clause must return FALSE, and the second call doesn't need to be made. This is often a source for confusion and errors.

In short, bitwise AND is the AND operator on series of bits, logical AND is the AND operator on statements returning boolean values with a left-to-right evaluation order.

EDIT: These operators in C are denoted as & (bitwise) and && (logical). Since they are only one character apart, and behave in very different manners, this is another source for common errors.

6. Aug 28, 2011

### MarcoD

Re: Logical AND and bitwise AND....dull mind

If you read my previous response it should be clear now.

The bitwise AND is used for AND-ing bit patterns. Commonly, it is used if the bits describe, for example, pixels or bit masks which denote permissions.

The logical AND is usually used for conditional/branching statements.

C was in some sense badly designed in that it doesn't have a type which denotes boolean values (TRUE or FALSE) commonly used for conditional/branching statements; it uses integers for that since the machines translated to commonly use registers (series of bits denoting integer values) for that. Therefor, logical and bitwise ANDs can (but shouldn't) be mixed in C programs. It's a design 'flaw' of the language.

As far as I know, a more modern language as Java does make a distinction. The bitwise AND will operate on integers interpreted as series of bits. The logical AND will operate on statements denoting boolean values, commonly used in branching statements.

Last edited by a moderator: Aug 28, 2011
7. Aug 28, 2011

### rama1001

Re: Logical AND and bitwise AND....dull mind

Thank you so much for explanation. I undestand it now.

8. Aug 28, 2011

### phinds

Re: Logical AND and bitwise AND....dull mind

Ah, I understand now from marco's post what you meant to "change from language to language".

As he explained, "bitwise and" and "logical and" are not even remotely the same operators and do not have the same meaning at all but DO have an unfortunate similarity that makes them easy to confuse.

As marco also pointed out (and what I didn't think about in my first post) is that the computer language symbols for these DO vary from language to language and in some language are can even be the SAME symbol, with the meaning being deciphered by the language compiler based on the context (the kind of variables being operated on).

There was even one language a long time ago called APL which specifically and deliberately used a single symbol BECAUSE it defined all operators by context. APL was also very weird because unlike any other computer language it evaluated EVERYTHING right-to-left, which made for lots of mistakes until you got used to it.

9. Aug 28, 2011

### rcgldr

Re: Logical AND and bitwise AND....dull mind

APL only has logical AND and OR, you'll get a domain error if you try to use a number other than 0 or 1 for input. For bitwise operations, you need to use encode (looks like ┬ ) and decode (looks like ┴ ) to convert to binary and back.

I think the C standard was updated so that logical operators (and comparasons) output 0 and 1, but will accept zero for false and non-zero for true as input.

C also has what I and others (Kernighan and Ritchie ... ) think is a precedence flaw in that bitwise AND and OR are lower precedence than logical AND and OR. See post #5 of this old thread:

Last edited: Aug 28, 2011
10. Aug 28, 2011

### phinds

Re: Logical AND and bitwise AND....dull mind

Guess I mis-remembered about APL, thanks.

Yeah, I would agree that any language (or implementation) that puts bitwise AFTER logical is just wrong.

11. Sep 1, 2011

### Jaynte

Re: Logical AND and bitwise AND....dull mind

Pseudo code for AND (&&)

Code (Text):

bool x[N];   //N = number of bits in vector to be AND:ed
bool y[N];

bool result = false;  //Boolean result

for(i=0;i<N;i++){
if(x[i] & y[i])              //Bit AND
result = true;
}

The result is ONLY false when none of the bits in the vector are 1 after the AND operation. i.e the result is 00000... after the AND operation.

Bitwise AND (&)
Code (Text):

bool x[N];
bool y[N];

bool result[N];    //The result will be a vector

for(i=0;i<N;i++)
result = x[i] & [y];

Note: C doesn't have a bool type like C++, but can be defined for example as an enum.
Code (Text):

typedef enum{false, true} bool;

This will give false = 0, true = 1

Last edited: Sep 1, 2011
12. Sep 1, 2011

### rcgldr

Re: Logical AND and bitwise AND....dull mind

For C and C++, all non-zero inputs are considered to be true, and only a zero input is considered to be false. The output of a comparson or logical operation will be 0 for false and 1 for true. Logical AND is true if both numbers are non-zero. It doesn't matter which bits are non-zero. Note C and C++ allows logical and with any value type, int, float, double, pointer, ... .

Code (Text):

int LOGICAL_AND(x, y) // x can be any type, y can be any type
{
if( x == 0 ) return 0;
if( y == 0 ) return 0;
return 1;
}

Last edited: Sep 1, 2011
13. Sep 2, 2011

### Jaynte

Re: Logical AND and bitwise AND....dull mind

I'll take it back :) you're right. Don't know what I was thinking. The bitwise operation is right thou.