按位编程的&运算符返回错误值(CPP)

时间:2018-11-02 14:34:06

标签: c++ binary bit-manipulation

我在Visual Studio的cpp中有这段代码

((handrule1 - maskRule1[0]) & test)

所有变量都是无符号的 int 。 它们的值分别是

  • 66848250
  • 50138096
  • 0x80808080。

我一直得到值作为该行的结果,这是不可能的。

这是怎么回事?

我已经尝试使用 long unsigned 变量来代替。

我猜测选择数据类型时可能做错了其他事情。

在下面您可以找到我的完整代码。

一些变量没有定义,但这是因为它们已经在我们不应该使用的另一个 cpp 文件中定义。

void init(void) {
    int aa, ab, l, x = 0;
    for (int i = 4; i <= 13; i++) {
        aa = 13 - i;
        for (int j = (aa + 2) / 3; j <= i && j <= aa; j++) {
            ab = aa - j;
            for (int k = (ab + 1) / 2; k <= j && k <= ab; k++) {
                adj[x] = i + j - 8;
                l = ab - k; code[x++] = (((i - 4) * 7) + j) * 5 + k;
                //              printf("%d %d %d %d: %d\n", i, j, k, l, (((i-4)*7)+j)*5+k);
            }
        }
    }
    return;
}


char countSetBits(long long unsigned n)
{
    if (n == 0)
        return 0;
    else
        return 1 + countSetBits(n & (n - 1));
}


void init_set(void) {
    long long unsigned hand = 0;
    char honorPoints = 0;
    char nrOfSpades = 0;
    char nrOfHearts = 0;
    char nrOfDiamonds = 0;
    char nrOfClubs = 0;
    long long unsigned maskAces = 0x8004002001;
    long long unsigned maskKings = 0x10008004002;
    long long unsigned maskQueens = 0x20010008004;
    long long unsigned maskJacks = 0x40020010008;
    long long unsigned maskSpades = 0x1FFF;
    long long unsigned maskHearts = 0x3FFE000;
    long long unsigned maskDiamonds = 0x7FFC000000;
    long long unsigned maskClubs = 0xFFF8000000000;

    char upperbound[RU][4];
    char lowerbound[RU][4];
    unsigned int handrule1 = 0;
    unsigned int handrule2 = 0;
    unsigned int handrule3 = 0;
    unsigned int maskRule1[RU];
    unsigned int maskRule2[RU];
    unsigned int maskRule3[RU];
    unsigned int maskInverse = 0x00FF00FF;
    unsigned int test = 0x80808080;
    unsigned int result1 = 0;
    bool applicableRule = false;

    unsigned int fuck = 0xFF936636;

    result1 = fuck & test;



    for (int r = 0; r < nrr; r++)
    {
        for (int i = 0; i < 4; i++)
        {
            upperbound[r][i] = 13;
            lowerbound[r][i] = 0;
        }
    }

    for (int r = 0; r < nrr; r++)
    {
        if (res[r] != 0)
        {
            for (int i = 0; i < res[r]; i++)
            {
                upperbound[r][color[r][i]] = (char) nru[r][i];
                lowerbound[r][color[r][i]] = (char) nrl[r][i];
            }
        }
        maskRule1[r] = (((char) distl[r] << 24) | ((char) distu[r] << 16) | ((char) ahpl[r] << 8) | (char) ahpu[r]) ^ maskInverse;
        maskRule2[r] = ((lowerbound[r][0] << 24) | (upperbound[r][0] << 16) | (lowerbound[r][1] << 8) | upperbound[r][1]) ^ maskInverse;
        maskRule3[r] = ((lowerbound[r][2] << 24) | (upperbound[r][2] << 16) | (lowerbound[r][3] << 8) | upperbound[r][3]) ^ maskInverse;
    }


    int x[52], y, a;
    for (int i = 0; i < 52; i++) x[i] = i;
    srand(1);
    for (int i = 0; i < CRD; i++) {
        for (int j = 52; --j > 1;) {
            y = rand() % j;
            a = x[y]; x[y] = x[j]; x[j] = a;
        }

        for (int j = 0; j < 4; j++)
        {
            for (int k = 13 * j; k < 13 * (j + 1); k++)
            {
                hand |= 1LLU << x[k];
            }

            //Counting honorpoints
            honorPoints = (countSetBits(hand & maskAces) * 4) + (countSetBits(hand & maskKings) * 3) + (countSetBits(hand & maskQueens) * 2) + (countSetBits(hand & maskJacks) * 1);
            hp[i][j] = (char) honorPoints;
            honorPoints = 0;


            //Counting distributions
            nrOfSpades = countSetBits(hand & maskSpades);
            nrOfHearts = countSetBits(hand & maskHearts);
            nrOfDiamonds = countSetBits(hand & maskDiamonds);
            nrOfClubs = countSetBits(hand & maskClubs);

            std::array<char, 4> arrayTest = { nrOfSpades, nrOfHearts, nrOfDiamonds, nrOfClubs };
            std::sort(arrayTest.begin(), arrayTest.end());

            char p = arrayTest[3];
            char o = arrayTest[2];
            char m = arrayTest[1];
            int test = (((p - 4) * 7) + o) * 5 + m;

            for (int x = 0; x < 39; x++)
            {
                if (code[x] == test)
                {
                    dis[i][j] = (char) x;
                }
            }

            //Counting opening bids
            ahp[i][j] = hp[i][j] + adj[dis[i][j]];
            if (ahp[i][j] < 0) ahp[i][j] = 0;

            handrule1 = ((dis[i][j] << 24) | (dis[i][j] << 16) | (ahp[i][j] << 8) | ahp[i][j]) ^ maskInverse;
            handrule2 = ((nrOfSpades << 24) | (nrOfSpades << 16) | (nrOfHearts << 8) | nrOfHearts) ^ maskInverse;
            handrule3 = ((nrOfDiamonds << 24) | (nrOfDiamonds << 16) | (nrOfClubs << 8) | nrOfClubs) ^ maskInverse;

            printf("%u \n", handrule1);
            printf("%u \n", maskRule1[0]);



            for (int r = 0; r < nrr; r++)
            {
                if ((((handrule1 - maskRule1[r]) & test) == 0) && (((handrule2 - maskRule2[r]) & test) == 0) && (((handrule3 - maskRule3[r]) & test) == 0))
                {
                    cnt[bid[r]][j]++;
                    applicableRule = true;
                    break;
                }
            }

            if (applicableRule == false)
            {
                cnt[0][j]++;
            }

            applicableRule = false;
            handrule1 = 0;
            handrule2 = 0;
            handrule3 = 0;
            nrOfSpades = 0;
            nrOfHearts = 0;
            nrOfDiamonds = 0;
            nrOfClubs = 0;
            hand = 0;
        }
    }
    return;
}

0 个答案:

没有答案