Rezniaq
Rezniaq

Reputation: 115

Logical & bitwise AND

I am using bitwise operators to store some boolean values in a variable. I assume i'm storing them appropiately, although here are my assignments:

int bit = 0;
bit |= 1;
bit |= 2;
bit |= 4;
bit |= 8;

What i am unsure of is the checking part. I have a simple knowledge about the difference between logical and bitwise operators. Here's how i check the values:

if ((bit & 1) && (bit & 2) && (bit & 8)) {
    std::cout << "a" << std::endl;
} 
else {
    std::cout << "b" << std::endl;  
}

I want to know if that kind of conditional is correct (i've done some tests, but i might be missing something) and also i want to know if i can check multiple bits at the same time, for example:

if (bit && (1 & 2 & 8) {
    std::cout << "a" << std::endl;
} 
else {
    std::cout << "b" << std::endl;  
}

I know that the last won't work as desired (at least that's what tests gave me), but i wanted to illustrate my idea.

Upvotes: 0

Views: 596

Answers (5)

ABDUL KADER M
ABDUL KADER M

Reputation: 1

Difference between both are simple &&-First statement is false means it cannot check second value &-It is check both the value either the first value is true or false

Upvotes: 0

R Sahu
R Sahu

Reputation: 206557

i want to know if i can check multiple bits at the same time

Yes, you can do that but your code is not correct.

  1. 1 & 2 & 8 will always be zero. You need to use 1 | 2 | 8.
  2. bit && (1 & 2 & 8) is not correct because of the above.

You can use:

if ( (bit & (1 | 2 | 8)) == (1 | 2 | 8) ) {
    std::cout << "a" << std::endl;
} 
else {
    std::cout << "b" << std::endl;  
}

The expression (bit & 1) && (bit & 2) && (bit & 8) is logically the same as the expression (bit & (1 | 2 | 8)) == (1 | 2 | 8)

See it working at https://ideone.com/KdGjiO.

Upvotes: 2

Shine
Shine

Reputation: 103

The bitwise AND operator compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

Since the AND operation if you want to check n. bit of the value you must AND it with 2^(n-1). If the bit is set the result of the bitwise operation will be greater than zero that means value is logically true otherwise it will be zero (or logically false)

if ((bit & 1) && (bit & 2) && (bit & 8))

this expression is suits what you want to do

if (bit && (1 & 2 & 8))

but 1 & 2 & 8 will produce always zero. The correct expression is:

if ((bit & (1 | 2 | 8)) == (1 | 2 | 8))

Upvotes: 2

Solomon Slow
Solomon Slow

Reputation: 27115

Here's an alternative implementation of your first example:

static const char * const table[] = {
    "a", "a", "a", "a",
    "a", "a", "a", "a", 
    "a", "a", "a", "b",
    "a", "a", "a", "b", 
};

std::cout << table[bit] << std::endl;

Of course, it might be safer to do the lookup as table[bit&0xF] instead.

Upvotes: -1

Carl
Carl

Reputation: 2067

You could use a binary literal to compare:

#include <iostream>

int main() {

    int bit = 0;
    bit |= 1;
    bit |= 2;
    bit |= 4;
    bit |= 8;

    if ((bit & 0b1111) == 0b1111) {
        std::cout << "YES!";
    }

    return 0;
}

Or as a function:

bool compareIntBool(const int bit, const int compareTo) {
    return (bit & compareTo) == compareTo ? true : false;
}

Then call it with a binary literal:

if (compareIntBool(bit, 0b1111)) {
    std::cout << "YES";
}

Upvotes: 1

Related Questions