Search code examples
javac#bitsetbitarray

Equivalent of c# BitArray.CopyTo of java BitSet


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) >> 3];
        mask.CopyTo(tmp, 0);
        return tmp;
    }

Solution

  • 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().