如何将一串零和一转换为位集? [英] How to translate a string of zeros and ones into bitset?

查看:119
本文介绍了如何将一串零和一转换为位集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有以下代码片段将字符串转换为位集.

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);
        }
    }

我的问题是,在霍夫曼代码的第一个和最后一个索引为0的情况下,如何确定位集的边界/大小/长度?

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 ?

推荐答案

以下位集按顺序包含[0,1],以下代码的最后一行将打印出2,即位集的长度.

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());

这篇关于如何将一串零和一转换为位集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆