-3

I saw some code like this:

void testCase2 (int variant)
{    
  if (variant & 0x1)
  {
    return;
  }
}

What does the & operator mean in the if statement, is it ==? Why to use &?

ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
user4016367
  • 431
  • 2
  • 9
  • 22

4 Answers4

1

It's a "bitwise and", basically that code will check if the lowest significant bit in 'variant' is set.

The operator does an AND operation on each relative bit of the two items being compared to get the result. It's a common way to examine variables that are being used to carry multiple bit 'flags'.

as an example, "bitwise anding" two variables with these binary representations:

00010001
00000001

would give:

00000001

http://en.wikipedia.org/wiki/Bitwise_operation#AND

Grimm The Opiner
  • 1,778
  • 11
  • 29
1

& is the bitwise AND operator. Given two integer operands, it does an AND operation on each bit position, i.e. in the result only those bits will be set that were set in both operands.

If one of the operands is 0x1 as in this case, the result will be 0x1 if, and only if, that bit is also set in the other operand (here, variant).

As C/C++ considers any non-zero integer to be true,

if (variant & 0x1)

checks if the least-significant bit in variant is set.

Similarly,

if (variant & 0x2)

would check if the second least-significant bit in variant is set, and

if (variant & 0x3)

would check if either of the two least-significant bits in variant is set.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
0

& operator is the boolean arithmetic AND operator i.e. bitwise AND

IN your code block, if (variant & 0x1) is evaluated as:

if (the result of ANDing variant and 0x1) is TRUE, them go inside the following block of code.

Consider the following code (see it in action on IDEONE):

#include <iostream>
using namespace std;

int main() {
    int variant = 0x1f; // Declared in HEX format;

    if (variant & 0x1){ // If the LSB is 1 i.e. if the 
        cout << "axious!!!";  // It'll print "axious!!!"
    } else
    {
        cout << "It's something else!!!";
    }

    return 0;
}

But the following code:

#include <iostream>
using namespace std;

int main() {
    int variant = 0x10; // Declared in HEX format;

    if (variant & 0x1){ // If the LSB is 1
        cout << "axious!!!";
    } else
    {
        cout << "It's something else!!!"; // This will be printed!!
    }

    return 0;
}

Now remember that 0x1 will be 1 in binary i.e. the LSB will be 1 so you are actually checking for what is the first bit of your variant.

ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
0
variant:      1011   Bitwise AND         
  0x1  :      0001
            -------
              0001
            -------

Bitwise AND is used to Turn-Off bits.

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.

SRC : refer here

theRoot
  • 571
  • 10
  • 33