zhangyunfeng
zhangyunfeng

Reputation: 21

What is the difference between 'and' and '&' in C++?

#include <iostream>
int main(int argc, char* argv[])
{
    unsigned long mask = 0x00000001;
    unsigned long mask1 = 0x00000001;
    unsigned long mask2 = 0x00000010;
    if ((mask and mask1) && (mask and mask2))// CONDITION_1 is True.
        std::cout << "Ohhhhhhh..." << std::endl;
    if ((mask & mask1) && (mask & mask2)) //CONDITION_2 is False.
        std::cout << "No Output..." << std::endl;
    return 0;
}

I think CONDITION_1 and CONDITION_2 both are False, but my thinking is wrong obviously , why 'and' and '&' are not same in C++?

Upvotes: 1

Views: 552

Answers (8)

Naseef Chowdhury
Naseef Chowdhury

Reputation: 2464

AND is equivalent to && which is regarded as logical operator. And & is bitwise operator.

Bitwise AND(&)

The bitwise AND operator is a single ampersand: &. A handy mnemonic is that the small version of the boolean AND, &&, works on smaller pieces (bits instead of bytes, chars, integers, etc). In essence, a binary AND simply takes the logical AND of the bits in each position of a number in binary form.

For instance, working with a byte (the char type):

01001000 
& 
10111000 
--------
00001000

The most significant bit of the first number is 0, so we know the most significant bit of the result must be 0; in the second most significant bit, the bit of second number is zero, so we have the same result. The only time where both bits are 1, which is the only time the result will be 1, is the fifth bit from the left. Consequently,

72 & 184 = 8

Logical AND(&&) The logical AND operator is used to test whether both conditions are true. If both conditions are true, logical AND returns true. Otherwise, it returns false.

Upvotes: 0

Edward Strange
Edward Strange

Reputation: 40897

and and && are the same. It's logical and. & is bitwise and.

Upvotes: 12

Pixelchemist
Pixelchemist

Reputation: 24956

& is the binary AND operator. The result of this operator has only those bits set to 1 that are 1 in both arguments.

The result of ((mask and mask1) && (mask and mask2):

  • mask and mask1 == true (== mask && mask1)
  • mask and mask2 == true (== mask && mask2)
  • (true) && (true) == true

The result of (mask & mask1) && (mask & mask2):

  • mask & mask1 == 0x00000001 (since the least significant bit is 1 in both cases)
  • mask & mask2 == 0x00000000 (the bits that are 1 in mask are 0 in mask2 and vice versa)
  • (0x00000001) && (0x00000000) == false

Upvotes: 0

shenles
shenles

Reputation: 582

The 2 "and" operators in C/C++ are && (logical and) and & (bitwise and).

&& will return a boolean result (true/false, 1/0) if both arguments are non-zero (true) and false otherwise. This is used to determine if 2 boolean conditions are BOTH true.

& will return a integer with any bits set (1) in both arguments. So 0b10101010 & 0b11110000 will produce 0b10100000. This is useful for checking flags, or any other uses for bitmasks (especially in the embedded world, where you might use individual bits instead of full bytes/words/dwords for flags).

Edit: learned something new and removed an incorrect statement.

Upvotes: 1

Mike Seymour
Mike Seymour

Reputation: 254721

and or && is the logical AND operator. It yields true if both operands convert to true.

bitand or & is the bitwise AND operator. Each bit of the result is set if the corresponding bits of both operands are set.

Upvotes: 5

Eric Finn
Eric Finn

Reputation: 9015

&& and and are both logical and operators, while & is the bitwise and operator.

So

(mask and mask1) && (mask and mask2)

is equivalent to

(mask && mask1) && (mask && mask2)

Upvotes: 1

AllTooSir
AllTooSir

Reputation: 49432

AND is logical short circuit && , while & is bitwise operator , working on individual bits.

When using the logical operators, C++ only evaluates what is necessary from left to right to come up with the combined relational result, ignoring the rest.

Bitwise operators modify variables considering the bit patterns that represent the values they store.

Here is some documentation.

Upvotes: 0

Jim McNulty
Jim McNulty

Reputation: 116

The single ampersand is a bitwise and while the 'and' keyword is an alternative for &&, a logical and.

Upvotes: 1

Related Questions