1

I create a BitSet of length 3 and clear the first two positions and set the last one. My debug output of the BitSet is expected to be something like {0, 0, 1}. But actually is {2}.

Implemented complete mvn project with JUnit 4.11 supporting Testcase.

package org.no_ip.leder.test;

import static org.junit.Assert.assertEquals;

import org.junit.BeforeClass;
import org.junit.Test;

import java.util.BitSet;

public class TestCase1 {

@BeforeClass 
public static void setUpClass() {      
    System.out.println("TestCase1 setup");
}

@Test
public void test1() {

    BitSet ex_result = new BitSet(3);
    BitSet test = new BitSet(3);

    App app = new App();

    test = app.createFromString("100");

    ex_result.clear(0); 
    ex_result.clear(1);
    ex_result.set(2);


    //DEBUG:
    System.out.println(ex_result);
    System.out.println(test);

    assertEquals("app.createFromString(\"100\"): ", ex_result, test);

}
}    

------------------------Output:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.no_ip.leder.test.TestSuite1
TestCase1 setup
{2}
{2}
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.231 sec

What is a Boolean BitSet representing with value of "2"?

Leder
  • 396
  • 1
  • 5
  • 21
  • 5
    See its `toString()` description here : https://docs.oracle.com/javase/8/docs/api/java/util/BitSet.html#toString-- . – Arnaud Feb 13 '19 at 08:20
  • 1
    [Java.util.BitSet.toString() Method](https://www.tutorialspoint.com/java/util/bitset_tostring.htm) – Ole V.V. Feb 13 '19 at 08:30
  • Possible duplicate of [How to convert BitSet to binary string effectively?](https://stackoverflow.com/questions/34748006/how-to-convert-bitset-to-binary-string-effectively) – M. Prokhorov Feb 13 '19 at 11:36

1 Answers1

1

The toString() implementation of BitSet returns the decimal representation of all indexes where a bit is set, not the binary representation of the Set, hence your output.

See the the official java docs as @Arnaud pointed out in the comments.

What is a Boolean BitSet representing with value of "2"?

Exactly what you did in your code, it's a BitSet where only the Bit with index 2 is set.

T A
  • 1,677
  • 4
  • 21
  • 29