Search code examples
javadata-structuresbinarybitset

How to store the zeros at the end of a binary sequence into a BitSet (Java)?


My aim is to store a binary sequence into a BitSet as it is, without loosing the track of any zero at the end of the sequence and further retrieve the sequence from the BitSet in the same order.

Example: if there is a sequence s = 00110100,
all the 8 bits of the sequence must be stored in the BitSet and must be retrieved in the same order.


Solution

  • http://docs.oracle.com/javase/7/docs/api/java/util/BitSet.html#BitSet(int)

    You will not loose any info if you ignore leading zeros, just keep track of number of bits you read. I guess you need to be able to print leading zeroes. You can do that by padding with 0s in printf function.

    String.format("%05d", yournumber); this will give you 5 leading zeroes.

    http://download.oracle.com/javase/7/docs/api/java/util/Formatter.html