How to translate a string of zeros and ones into bitset?

邮差的信 提交于 2019-12-25 07:48:22

问题


So I have this code snippet to translate a string into bitset.

    String huffmancode = "0010110100";
    char[] ch = huffmancode.toCharArray();

    BitSet bs = new BitSet();
    for (int i = 0; i < ch.length; i++)  {
        if (ch[i] == '1') {
            bs.set(i);
        }
    }

My question is how to determine the boundary / size / length of the bitset given that the first and the last indexes of huffman code were 0's ?


回答1:


The following bitset contains [0,1] in order and the last line of the following code prints out 2, the length of the bitset.

BitSet bs = new BitSet();

bs.set(0, false);
bs.set(1, true);

System.out.println(bs.length());


来源:https://stackoverflow.com/questions/22315371/how-to-translate-a-string-of-zeros-and-ones-into-bitset

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!