Understanding Use & Bitwise Operator

I analyzed the code and tried to understand every part of the algorithm. I came across this part where the bitwise and operator were used

if (qpd >= 0) qpd += qpd&1;
else qpd -= qpd&1;

From what I understand, the algorithm wants to use character 1 as a mask for the qpd variable, but since the number is 1, there is no point in doing an operation that makes almost no changes. Please enlighten me here since I am lost.

A closed loop that receives the magnitude and frequency of data from an array that uses a bitwise operator looks like this:

/* this is the analysis step */
            for (k = 0; k <= fftFrameSize2; k++) {

                /* de-interlace FFT buffer */
                real = gFFTworksp[2*k];
                imag = gFFTworksp[2*k+1];

                /* compute magnitude and phase */
                magn = 2.*sqrt(real*real + imag*imag);
                phase = atan2(imag,real);

                /* compute phase difference */
                tmp = phase - gLastPhase[k];
                gLastPhase[k] = phase;

                /* subtract expected phase difference */
                tmp -= (double)k*expct;

                /* map delta phase into +/- Pi interval */
                qpd = tmp/M_PI;
                if (qpd >= 0) qpd += qpd&1;
                else qpd -= qpd&1;
                tmp -= M_PI*(double)qpd;

                /* get deviation from bin frequency from the +/- Pi interval */
                tmp = osamp*tmp/(2.*M_PI);

                /* compute the k-th partials' true frequency */
                tmp = (double)k*freqPerBin + tmp*freqPerBin;

                /* store magnitude and true frequency in analysis arrays */
                gAnaMagn[k] = magn;
                gAnaFreq[k] = tmp;

            }
+3
source share
2 answers

The code is used to round numbers to the next even value; rounding positive numbers up and negative numbers down: e.g.

7 becomes 8
12 remains 12
-7 becomes -8

, 1 0 .

+7

, , , , , &, , , qbd ( , ), .

, , .

+1

All Articles