Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8230557

wrong number of bits of space in use returned when BitSet#size() is called

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      Because the return type of the method BitSet#size is int, so the method may return a negative value in some case, for example, will return -2147483648.
      ```
      BitSet bitSet = new BitSet(Integer.MAX_VALUE);
      for (int i = 0; i < Integer.MAX_VALUE - 1000; i++) {
          bitSet.set(i);
      }
      System.out.println(bitSet.size());
      ```

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      BitSet bitSet = new BitSet(Integer.MAX_VALUE);
      for (int i = 0; i < Integer.MAX_VALUE - 1000; i++) {
          bitSet.set(i);
      }
      System.out.println(bitSet.size());

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      137438953408
      ACTUAL -
      -2147483648

      ---------- BEGIN SOURCE ----------
      public class TestBitSetSize {

          public static void main(String[] args) throws Exception {

              java.util.BitSet bitSet = new java.util.BitSet(Integer.MAX_VALUE);
              for (int i = 0; i < Integer.MAX_VALUE - 1000; i++) {
                  bitSet.set(i);
              }
              System.out.println(bitSet.size());
          }

      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      change the return type int to long

      FREQUENCY : always


            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: