将C ++按位运算移植到Scala按位运算

时间:2020-03-05 07:33:40

标签: c++ scala bitwise-operators

我有一个遗留的C ++代码,其中涉及许多按位运算。我必须将所有这些东西移植到Scala。这里是一些图书馆之类的简单方法

编辑: C ++代码如下:

const
    getSale = (data, locations) => {
        const sales = data.reduce((r, o) => (r[o.location] = o, r), {});
        return locations.map(l => sales[l] || sales.Null);
    },
    data = [{ location: "Phnom Penh", sale: 1000 }, { location: "Kandal", sale: 500 }, { location: "Takeo", sale: 300 }, { location: "Kompot", sale: 700 }, { location: "Prey Veng", sale: 100 }, { location: "Seam Reap", sale: 800 }, { location: "Null", sale: 0 }],
    locations = ['Phnom Penh', 'AA', 'Kompot', 'BB'],
    result = getSale(data, locations);

console.log(result);

c,g,h,w,s是输入。

我将代码移植为Scala(与C ++代码中的常量相同):

.as-console-wrapper { max-height: 100% !important; top: 0; }

现在要测试此代码,我的测试代码为:

static uint64_t const C_MASK = 0x3FF;
static uint64_t const C_OFFSET = 46;
static uint64_t const G_MASK = 0x3FF;
static uint64_t const G_OFFSET = 46;
static uint64_t const W_MASK = 0x1FF;
static uint64_t const W_OFFSET = 16;
static uint64_t const H_OFFSET = 25;
static uint64_t const S_MASK = 0xF;
static uint64_t const S_OFFSET = 10;

result |= ((c & C_MASK) << C_OFFSET)
        | ((g & G_MASK) << G_OFFSET)
        | ((h & W_MASK) << H_OFFSET)
        | ((w & W_MASK) << W_OFFSET)
        | ((s & S_MASK) << S_OFFSET);

我得到的值是:

c = 0,g = 768,w = 0,h = 0,s = 3

除了s之外,其他所有内容都与我传递给函数的内容不同。

我不确定我在这里犯了什么错误。

0 个答案:

没有答案