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

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

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 21
    • 21
    • 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"?

            asotona Adam Sotona
            shade Aleksey Shipilev
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: