jdk.internal.classfile.impl.EntryMap::nextPowerOfTwo math problem

XMLWordPrintable

    • Type: Bug
    • Resolution: Fixed
    • Priority: P4
    • 21
    • Affects Version/s: 21
    • Component/s: core-libs

      I noticed the math problem in jdk.internal.classfile.impl.EntryMap::nextPowerOfTwo:

      % jshell
      | Welcome to JShell -- Version 19.0.2
      | For an introduction type: /help intro

      jshell> public static long nextPowerOfTwo( long x ) {
         ...> x = -1 >>> Long.numberOfLeadingZeros(x - 1);
         ...> return x + 1;
         ...> }
      | created method nextPowerOfTwo(long)

      jshell> nextPowerOfTwo(0)
      $2 ==> 0

      jshell> nextPowerOfTwo(1)
      $3 ==> 0

      jshell> nextPowerOfTwo(2)
      $4 ==> 2

      jshell> nextPowerOfTwo(3)
      $5 ==> 4

      Note how "1" yields "0" as the next power of two.

      Also check if you really need "next" power of two, which should give next(2) = 4. Maybe this is just "round"?

            Assignee:
            Adam Sotona
            Reporter:
            Aleksey Shipilev
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: