Java BitSet which allows easy Concatenation of BitSets

时光毁灭记忆、已成空白 提交于 2019-12-01 15:57:32

Well there's no way to implement this terribly efficient (performance and memory that is) since there's no leftshift method.

What you can do is either use the obvious nextSetBit for loop - slow, but memory efficient.

The presumably faster method would be to use toLongArray on one, copy that correctly shifted into a large enough array, create a bitset from that and or it with the other. That way you don't do any bitshifting on single bits but instead work on wordsized chunks.

This worked for me:

BitSet concatenate_vectors(BitSet vector_1_in, BitSet vector_2_in) {
  BitSet vector_1_in_clone = (BitSet)vector_1_in.clone();
  BitSet vector_2_in_clone = (BitSet)vector_2_in.clone();
  int n = 5;//_desired length of the first (leading) vector
  int index = -1;
  while (index < (vector_2_in_clone.length() - 1)) {
    index = vector_2_in_clone.nextSetBit((index + 1));
    vector_1_in_clone.set((index + n));
  }
  return vector_1_in_clone;
}

Result: 11111010101

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