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

No way to determine # of bits in basic types

    XMLWordPrintable

Details

    • Enhancement
    • Resolution: Duplicate
    • P5
    • None
    • 1.2.0
    • core-libs
    • generic
    • generic

    Description



      Name: clC74495 Date: 09/10/98


      There seems to be no predefined constants in Java for determining the number of bits
      in various integral types. For instance, in C, I can #include <limits.h> and then use
      constants such as CHAR_BITS to indicate the number of bits in a 'char' value. In Java,
      I have to define my own local constant do do something similar, and my own local
      constant will almost certainly have a different name from the one used by other Java
      developers. Or, I can just hardcode the number, which usually leads to
      difficult-to-understand code.

      I would like to see constants added to each of the numeric 'wrapper' classes, like so:

      class Integer
      {
         ...
         public static final BITS = 32;
      }
      class Byte
      {
         public static final int BITS = 8;
      }
      etc. for classes Long, Short and Char.

      This would allow easy writing of code such as:
      byte [] bytes = whatever;
      int getBitAt(int bitIndex)
      {
          return ((bytes[bitIndex / Byte.BITS]
                   >> (bitIndex % Byte.BITS)) & 1;
      }
      rather than encouraging people to write code such as:
      int getBitAt(int index)
      {
          return (data[x / 8] >> (x % 8)) & 1;
      }

      As a somewhat seperate issue, it seems to me that this information would also
      be useful as a virtual method inherited from class Number.
      (Review ID: 38581)
      ======================================================================

      Attachments

        Issue Links

          Activity

            People

              apalanissunw Anand Palaniswamy (Inactive)
              clucasius Carlos Lucasius (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: