Re: newbie lost in strings......

From: Grant Wagner (gwagner_at_agricoreunited.com)
Date: 07/30/04


Date: Fri, 30 Jul 2004 16:25:45 GMT

The reason you gave is why a byte can contain the values -128 to 127.

The reason I gave is why Byte.MAX_VALUE returns 127. The only reason Byte returns
127 is because someone at Sun wrote:

public final class Byte extends Number implements Comparable {

     /**
      * A constant holding the maximum value a <code>byte</code> can
      * have, 2<sup>7</sup>-1.
      */
     public static final byte MAX_VALUE = 127;

If someone suddenly decided a primitive byte in the VM was 12 bits wide, Byte would
have be re-written to be:

public static final int MAX_VALUE = 2047;

(which would require casts anywhere Byte.MAX_VALUE is used)

The important point is there is no "magic" going on here, Byte.MAX_VALUE is just
returning a public static final byte that contains a particular value. By happy
coincidence (actually design) that particular value happens to match the maximum
value a primitive byte can hold, but Byte doesn't *know* that.

Carl Smotricz wrote:

> I'd like to take a crack at answering why byte goes to 127!
>
> A byte (in the general, not the Java sense) is usually an 8 bit
> quantity; the 8 bits can be thought of as representing the positive
> integers from 0 (no bits set) to 255 (all 8 bits set).
>
> Java, however, rules that all integral primitive types (byte, int and
> long) shall hold *signed* values. This means one bit is used as a sign
> bit, with the advantage that those (in the case of byte) 8 bits can now
> hold negative numbers as well, but the magnitude is a bit less. Since
> 2's complement is used for the representation, the values actually go
> from -128 to 127.
>
> Hope that helps.
>
> -Carl-

--
Grant Wagner <gwagner@agricoreunited.com>


Relevant Pages

  • Re: K&R2 1.6 Arrays, exercise 1-13 (vertical histogram)
    ... | int main ... I thought you always initialised variables on declaration, ... This, of course, could have been done by initialising on declaration ... (If the latter reason is true, learning it would have done no good here, because Reason 1 would still have applied.) ...
    (comp.lang.c)
  • Re: newbie (dropping numbers) new post
    ... Either use a #define or a constant int here. ... Please note that the above loop is only an example. ... entry error was detected on stdin. ... Reason number 2 for the array idea for scores. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Boost process and C
    ... of int for sizes, rather than size_t. ... read-only and constant strings and detecting errors efficiently ... I do have good reason for doing this. ... If the DOS port hadn't been dropped then depending on the compiler we ...
    (comp.lang.c)
  • Re: what is wrong?
    ... The only reason you "should" get no output is if the int 21h/47h fails. ... After the "xlatb", ...
    (alt.lang.asm)
  • Re: clearing low order bits / making a multiple of power of 2
    ... > The other reason has to do with correctness. ... > The reason is that, on a platform where 'int' is 64 bits, ... > 'unsigned int' operands. ... > and developers who are careful concern themselves with such ...
    (comp.lang.c)