Logical AND and bitwise AND dull mind

  • Thread starter rama1001
  • Start date
  • Tags
    Mind
In summary, logical AND and bitwise AND are two different operators that serve different purposes. Logical AND is used for conditional/branching statements, while bitwise AND is used for AND-ing bit patterns. They can be confused since they are commonly used in languages like C that don't have a type for boolean values, resulting in the use of integers instead. It is important to use the correct operator for the intended purpose to avoid errors.
  • #1
rama1001
132
1
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.
 
Technology news on Phys.org
  • #2


rama1001 said:
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.

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


rama1001 said:
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.

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


MATLABdude said:
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.


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


rama1001 said:
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.

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


rama1001 said:
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.

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.

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

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:
  • #7


Thank you so much for explanation. I undestand it now.
 
  • #8


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


phinds said:
APL which specifically and deliberately used a single symbol
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:

https://www.physicsforums.com/showthread.php?t=436196
 
Last edited:
  • #10


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


Pseudo code for AND (&&)

Code:
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:
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:
typedef enum{false, true} bool;

This will give false = 0, true = 1
 
Last edited:
  • #12


Jaynte said:
Pseudo code for AND (&&) ... (code showing bitwise anding of all bits) ...
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:
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:
  • #13


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

1. What is the difference between logical AND and bitwise AND?

Logical AND is used to determine whether two conditions are both true or false, and returns a boolean value. Bitwise AND, on the other hand, performs the AND operation on each bit of two numbers, and returns a new number as a result.

2. How is logical AND used in programming?

Logical AND is commonly used in programming to create conditional statements. It allows the program to execute a certain block of code only if both conditions are satisfied.

3. Can logical AND be used with more than two conditions?

Yes, logical AND can be used with multiple conditions. It will only return true if all conditions are true, otherwise it will return false.

4. What is the purpose of bitwise AND in programming?

Bitwise AND is used in programming for various purposes such as setting or clearing specific bits in a binary number, checking for the presence of certain flags, or performing certain arithmetic operations.

5. Are there any differences in the behavior of logical AND and bitwise AND in programming?

Yes, there are significant differences in the behavior of logical AND and bitwise AND. Logical AND always returns a boolean value, while bitwise AND returns a numerical value. Also, logical AND evaluates both conditions, while bitwise AND only evaluates necessary bits.

Similar threads

  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
12
Replies
397
Views
13K
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
875
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
1
Views
678
Back
Top