从数组中获取标志值的按位组合值

问题描述 投票:0回答:1

我有一个枚举:

enum States {
    Empty = 0,
    NoData = 0x1,
    UnChecked = 0x2,
    Review = 0x4
}

我已将它们加载到多选控件中,因此可以将值选择到数组中,例如:[0, 0x1, 0x2]

如何获得打字稿中按位组合的值(示例中为6)?

javascript typescript bit-manipulation
1个回答
1
投票

您在问如何进行按位组合吗?

const addition = [
  0x01,
  0x02,
  0x04,
].reduce((tmp, x) => tmp | x, 0);

console.log(`0x${addition.toString(16)}`);
© www.soinside.com 2019 - 2024. All rights reserved.