>
>
>
V753. The '&=' operation always sets a …


V753. The '&=' operation always sets a value of 'Foo' variable to zero.

The analyzer detected that applying a bitwise "AND" operator to a variable results in setting its value to zero, which is strange because a simpler way to get a null value is by using an assignment operation.

If this operation participates in a series of computations, it is likely to execute incorrectly – for example, it is applied to a wrong variable, or a wrong constant is used as the right operand because of a typo.

There are several scenarios when this warning is triggered.

The first case is when the operator is sequentially applied to a variable with unknown value and the right operand is represented by such constants that lead to the expression evaluating to zero:

void foo(int A)
{
   A &= 0xf0;
   .... 
   A &= 1;
   
   // 'A' now always equals 0.
}

Executing these two operations will result in a null value regardless of the initial value of the 'A' variable. This code probably contains an error, and the programmer needs to check the correctness of the constants used.

The second case deals with applying the operator to a variable whose value is known:

void foo()
{
   int C;
   .... 
   C = 1;
   ....
   C &= 2;
   
   // C == 0
}

In this case, the result is a null value, too. Like in the previous case, the programmer needs to check the correctness of the constants used.

The diagnostic can also be triggered by the following code, which is quite common:

void foo()
{
   int flags;
   .... 
   flags = 1;
   ....
   flags &= ~flags;
   ....
}

This technique is sometimes used by programmers to reset a set of flags. We believe that this technique is unjustified and may confuse your colleagues. A simple assignment is more preferable:

void foo()
{
   int flags;
   .... 
   flags = 1;
   ....
   flags = 0;
   ....
}

This diagnostic is classified as: