0

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.

Machavity
  • 30,841
  • 27
  • 92
  • 100
Rezniaq
  • 105
  • 1
  • 8
  • You, yourself get to decide if its correct. Should it be true if bits 1, 2, and 8 are set? If so, then it's correct. The `if (bit && (1 & 2 & 8))` proposal will never evaluate to true, under no circumstances, so it's obviously wrong. – Sam Varshavchik Sep 15 '17 at 21:01
  • Your last example should be `if (bit & (1|2|8))`. I think that would work. It would create a mask by OR'ing 1, 2, 8 together, then AND that with `bit`, if bit 1, 2 or 8 were set, it would be true. – Zan Lynx Sep 15 '17 at 21:06
  • Yes, i know that it is up to my own criteria. However, i wanted to know if it would work as required (and that's what my tests said) or if i should take other alternative. – Rezniaq Sep 15 '17 at 21:06
  • Perhaps see https://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit – Cody Gray - on strike Sep 15 '17 at 21:10
  • 1
    `if (bit & (1|2|8))` will evaluate as true if ANY of the 3 bits is set. If you want to check if ALL of the 3 bits are set, you have to use `if ((bit & (1|2|8)) == (1|2|8))` instead. – Remy Lebeau Sep 15 '17 at 21:15

5 Answers5

2

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.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • I've tried your example code and it doesn't work as expected. Today i used a similar code (instead of bit & (1... i wrote bit && (1...) but it didn't work either. If i change the 8 with a 16, it will print a anyways, wheter i **OR**ed it on my assignments or not. Shouldn't it print b if i use sixteen? – Rezniaq Sep 15 '17 at 21:11
  • @Rezniaq, I can't tell what could be wrong without a [mcve]. – R Sahu Sep 15 '17 at 21:14
  • I am using no more than what's written on my question. I replaced my if statement with yours, and changed your 8 to 16. If i copy my main it will be the same as rewriting my question, but i can do it if you want to. – Rezniaq Sep 15 '17 at 21:19
  • 1
    "*The expression `(bit & 1) && (bit & 2) && (bit & 8)` is logically the same as the expression `bit & (1 | 2 | 8)`*" - no, it is not. `bit & (1 | 2 | 8)` is logically the same as `(bit & 1) || (bit & 2) || (bit & 8)`. The correct expression would be `(bit & (1 | 2 | 8)) == (1 | 2 | 8)` instead. – Remy Lebeau Sep 15 '17 at 21:29
  • Yes, your code IS working, however, it doesn't accomplish my desired output. As i said, if i change your 8 with a 16, it will not work, but the original code will. Check it at ideone.com/npwtHQ – Rezniaq Sep 15 '17 at 21:32
  • @Rezniaq, the error was pointed out by Remy Lebeau. Try the updated logic. – R Sahu Sep 15 '17 at 21:35
  • @Rezniaq: `bit & (1 | 2 | 16)` is the same as `(bit & 1) || (bit & 2) || (bit & 16)`, which is not the same as `(bit & 1) && (bit & 2) && (bit & 16)`. There is a big difference between checking if ANY bits are set vs checking if ALL bits are set – Remy Lebeau Sep 15 '17 at 21:36
  • @Remy Lebeau @R Sahu Thank you both! – Rezniaq Sep 15 '17 at 22:22
2

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))
Shine
  • 103
  • 1
  • 4
  • 1
    You need to use `&` instead of `&&`. But note that `if (bit & (1|2|8))` will evaluate to true if ANY of the 3 bits is set. If you want to check if ALL of the 3 bits are set, you have to use `if ((bit & (1|2|8)) == (1|2|8))` instead. – Remy Lebeau Sep 15 '17 at 21:26
  • I haven't noticed the `&&` operator since I did copy/paste.Thanks for the correction, I have read the question again and the correct expression should be `if ((bit & (1|2|8)) == (1|2|8))` as you said. – Shine Sep 15 '17 at 21:32
  • Thank you very much! I would mark your answer as correct but i can't mark two. R Sahu's one also provides a direct answer to my first question (if i can check multiple bit at a time). I hope you don't get offended. – Rezniaq Sep 15 '17 at 22:27
1

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";
}
Carl
  • 2,057
  • 1
  • 15
  • 20
  • Although your answer doesn't directly address my question, it's something i didn't know before, so i'll thank you. I've never thought of nor seen binary literals. – Rezniaq Sep 15 '17 at 23:45
0

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

-1

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.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
  • The purpose of the question is to ask about how to use bitwise operators in general, not about how to output a value based on bits. This answer doesn't address the real question. – Remy Lebeau Sep 15 '17 at 21:25