You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让C++中AND与XOR组合运算时XOR优先执行?

自定义Binary类型实现XOR优先级高于AND的方案

问题场景

测试代码无法修改,要求a1 & a2 ^ a3等价于a1 & (a2 ^ a3),期望结果为0x8,但当前代码因原生运算符优先级(&高于^),实际计算为(a1 & a2) ^ a3得到0x5,且COMBO结构体存在运算逻辑错误。

核心问题

  1. C++原生运算符优先级不可修改,&默认比^高,编译器会优先解析a1 & a2。
  2. 原COMBO结构体错误使用+运算,而非目标&运算。
  3. 缺少必要的运算符重载,无法拦截并修正运算顺序。

解决方案

通过延迟计算+运算符重载拦截,让表达式在最终转换为Binary或unsigned int时,按照XOR优先的规则计算:

修改后的完整代码

Binary.h

struct Binary
{
    // ---Skip Constructor---

    struct XOR;
    struct AND;
    struct COMBO;

    XOR operator ^ (const Binary& r) const;
    AND operator & (const Binary& r) const;
    operator unsigned int() const;

    // Data: (do not add or modify the data)
    unsigned int x;
};

struct Binary::XOR
{
    XOR(const Binary& a, const Binary& b);
    operator Binary() const;
    operator unsigned int() const;
    COMBO operator & (const Binary& r) const;

    const Binary& a;
    const Binary& b;
};

struct Binary::AND
{
    AND(const Binary& a, const Binary& b);
    operator Binary() const;
    operator unsigned int() const;
    XOR operator ^ (const Binary& r) const;

    const Binary& a;
    const Binary& b;
};

struct Binary::COMBO
{
    COMBO(const Binary& lhs, const XOR& rhs);
    COMBO(const XOR& lhs, const Binary& rhs);
    operator Binary() const;
    operator unsigned int() const;

    const Binary* lhs_bin;
    const XOR* lhs_xor;
    const Binary* rhs_bin;
    const XOR* rhs_xor;
};

Binary.cpp

#include "Binary.h"

// Skip constructor and operator =

Binary::XOR Binary::operator ^ (const Binary& r) const
{
    return XOR(*this, r);
}

Binary::AND Binary::operator & (const Binary& r) const
{
    return AND(*this, r);
}

Binary::operator unsigned int() const
{
    return this->x;
}

// XOR结构体实现
Binary::XOR::XOR(const Binary& a, const Binary& b) : a(a), b(b) {}
Binary::XOR::operator Binary() const
{
    return Binary(a.x ^ b.x);
}
Binary::XOR::operator unsigned int() const
{
    return a.x ^ b.x;
}
Binary::COMBO Binary::XOR::operator & (const Binary& r) const
{
    return COMBO(*this, r);
}

// AND结构体实现
Binary::AND::AND(const Binary& a, const Binary& b) : a(a), b(b) {}
Binary::AND::operator Binary() const
{
    return Binary(a.x & b.x);
}
Binary::AND::operator unsigned int() const
{
    return a.x & b.x;
}
// 关键修正:拦截(a&b)^c,改为执行a&(b^c)
Binary::XOR Binary::AND::operator ^ (const Binary& r) const
{
    return XOR(Binary(a.x & (b.x ^ r.x)), Binary(0));
}

// COMBO结构体实现
Binary::COMBO::COMBO(const XOR& lhs, const Binary& rhs) 
    : lhs_xor(&lhs), rhs_bin(&rhs), lhs_bin(nullptr), rhs_xor(nullptr) {}
Binary::COMBO::COMBO(const Binary& lhs, const XOR& rhs) 
    : lhs_bin(&lhs), rhs_xor(&rhs), lhs_xor(nullptr), rhs_bin(nullptr) {}

Binary::COMBO::operator Binary() const
{
    unsigned int val = 0;
    if (lhs_xor && rhs_bin) {
        val = static_cast<unsigned int>(*lhs_xor) & rhs_bin->x;
    } else if (lhs_bin && rhs_xor) {
        val = lhs_bin->x & static_cast<unsigned int>(*rhs_xor);
    }
    return Binary(val);
}

Binary::COMBO::operator unsigned int() const
{
    unsigned int val = 0;
    if (lhs_xor && rhs_bin) {
        val = static_cast<unsigned int>(*lhs_xor) & rhs_bin->x;
    } else if (lhs_bin && rhs_xor) {
        val = lhs_bin->x & static_cast<unsigned int>(*rhs_xor);
    }
    return val;
}

效果验证

当执行answer = a1 & a2 ^ a3时:

  1. 编译器按原生优先级解析为(a1 & a2) ^ a3,得到AND对象与a3。
  2. 调用AND::operator^(const Binary&),该函数内部直接计算a1.x & (a2.x ^ a3.x),返回对应结果的XOR对象。
  3. XOR对象转换为Binary时,将计算结果赋值给answer,最终得到期望的0x8。

内容的提问来源于stack exchange,提问作者HermitGrey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 05:44:58