1
#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++?

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
  • 1
    possible duplicate of [Is there any difference between && and & with bool(s)?](http://stackoverflow.com/questions/6577504/is-there-any-difference-between-and-with-bools) – NominSim Jul 16 '13 at 16:51
  • Relevant previous threads: [Logical Operators in C](http://stackoverflow.com/questions/12332316/logical-operators-in-c) and [What are bitwise operators?](http://stackoverflow.com/questions/276706/what-are-bitwise-operators) Also see [Alternative operator representations](http://en.cppreference.com/w/cpp/language/operator_alternative) – Shafik Yaghmour Jul 16 '13 at 16:52
  • "What's the difference...?" Everything. – Mooing Duck Jul 16 '13 at 17:47
  • Freak out your colleagues: `const bitand int foo = 5;` :P – MSalters Jul 16 '13 at 19:38

8 Answers8

12

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

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
Edward Strange
  • 40,307
  • 7
  • 73
  • 125
5

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.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • I think `and` yields `true` iff both operands are non-zero (`true` token has a specific value), e.g. `(5 && 2)` yields `true` – franji1 Jul 16 '13 at 17:16
  • @franji1: It yields `true` iff both operands *convert to* `true`. For numeric types, that means they are non-zero; for pointer types, it means they are non-null; for user-defined types it means they have an overloaded conversion operator which returns `true` (or something that converts to `true`). – Mike Seymour Jul 16 '13 at 17:18
1

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

Jim McNulty
  • 116
  • 2
  • 11
1

&& 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)
Eric Finn
  • 8,629
  • 3
  • 33
  • 42
1

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.

shenles
  • 562
  • 5
  • 19
  • 4
    -1, `and` is a valid operator in C++. [See here](http://en.cppreference.com/w/cpp/language/operator_alternative) – Rapptz Jul 16 '13 at 16:49
  • @Rapptz thanks for the correction, I've never come across the text version of them before! Edited answer to remove incorrect stuff. – shenles Jul 16 '13 at 16:51
0

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.

Community
  • 1
  • 1
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
0

& 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
Pixelchemist
  • 24,090
  • 7
  • 47
  • 71
0

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.

Naseef Chowdhury
  • 2,357
  • 3
  • 28
  • 52