Java中与C#的BitArray.CopyTo等效的是BitSet的“`void copyTo(LongBuffer buffer)“`方法。

huangapple go评论99阅读模式
英文:

Equivalent of c# BitArray.CopyTo of java BitSet

问题

public byte[] translate(BitSet mask) {
    byte[] tmp = new byte[(mask.length() + 7) >>> 3];
    for (int i = mask.nextSetBit(0); i >= 0; i = mask.nextSetBit(i + 1)) {
        tmp[i >>> 3] |= (byte) (1 << (i & 7));
    }
    return tmp;
}
英文:

I want to translate the following c# code into java code, but i cannot find the equivalent of copyTo of Java BitSet

public byte [] translate(BitArray mask)
    {
        byte[] tmp = new byte[(mask.Length + 7) &gt;&gt; 3];
        mask.CopyTo(tmp, 0);
        return tmp;
    }

答案1

得分: 1

在Java中,您甚至不需要编写自己的方法来执行此操作 - 您可以直接调用BitSet#toByteArray。也就是说,在您的情况下,只需使用mask.toByteArray()

英文:

In Java, you wouldn't even need to write your own method to do that - you could just call BitSet#toByteArray. I.e., in your case, just use mask.toByteArray().

huangapple
  • 本文由 发表于 2020年4月10日 21:16:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/61141089.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定