byte type and addition operator

Discussion in 'Java' started by jk, Feb 14, 2005.

  1. jk

    jk Guest

    Hello: Is there an operator for directly adding bytes. I can do it
    by creating instances of Byte, and getting their intValue, but is
    there a direct method such as byte 'b = 2 + 3;'. I know that casting
    this to bytes will work: 'b = (byte)2 + (byte)3;', but is there an
    operator that does not require any conversion of types?

    Thanks in Advance
    John

    =====================
    Byte bA = new Byte((byte)65);
    Byte b1 = new Byte((byte)1);
    byte[] bytes = new byte[3];
    bytes[0] = bA.byteValue();
    bytes[1] = (byte)(bA.intValue() + b1.intValue()); // 66, B
    ....
    =====================
    jk, Feb 14, 2005
    #1
    1. Advertising

  2. (jk) writes:

    > is there an operator that does not require any conversion of types?


    No.
    Tor Iver Wilhelmsen, Feb 14, 2005
    #2
    1. Advertising

  3. jk

    Eric Sosman Guest

    jk wrote:
    > Hello: Is there an operator for directly adding bytes. I can do it
    > by creating instances of Byte, and getting their intValue, but is
    > there a direct method such as byte 'b = 2 + 3;'. I know that casting
    > this to bytes will work: 'b = (byte)2 + (byte)3;', but is there an
    > operator that does not require any conversion of types?


    The `+' operator converts its byte operands to int and
    produces an int sum, so conversion has already occurred before
    you get a chance to do anything about it. You need to convert
    this int sum to a byte before you can store it in `b'. (And
    note that the example you "know ... will work" doesn't.)

    --
    Eric Sosman, Feb 14, 2005
    #3
  4. jk wrote:

    > Hello: Is there an operator for directly adding bytes. I can do it
    > by creating instances of Byte, and getting their intValue, but is
    > there a direct method such as byte 'b = 2 + 3;'. I know that casting
    > this to bytes will work: 'b = (byte)2 + (byte)3;', but is there an
    > operator that does not require any conversion of types?
    >
    > Thanks in Advance
    > John
    >
    > =====================
    > Byte bA = new Byte((byte)65);
    > Byte b1 = new Byte((byte)1);
    > byte[] bytes = new byte[3];
    > bytes[0] = bA.byteValue();
    > bytes[1] = (byte)(bA.intValue() + b1.intValue()); // 66, B
    > ...
    > =====================


    The arithmetic is all done as int so you will have to cast it back to a
    byte.

    byte a = 23;
    byte b = -48

    byte c = (byte)(a + b);

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Feb 14, 2005
    #4
  5. jk wrote:

    > Hello: Is there an operator for directly adding bytes. I can do it
    > by creating instances of Byte, and getting their intValue, but is
    > there a direct method such as byte 'b = 2 + 3;'. I know that casting
    > this to bytes will work: 'b = (byte)2 + (byte)3;', but is there an
    > operator that does not require any conversion of types?


    I think you're confused. The + operator works on any combination of
    numeric primitives just fine without casting the operands. You may,
    however, need to cast the result, which will always be of type int,
    long, float, or double, depending on the operand types. One can argue
    that the behavior is appropriate (it avoids overflow problems when
    adding the small integer data types), but I think the real reason for it
    was to reduce the number of bytecodes that needed to be implemented.

    So, the following are legal:

    byte b = (byte) (2 + 3);
    byte c = (byte) (2 + 17000);
    byte d = (byte) (b + c);
    int i = b + c + (short) 15000 + 'q';

    But the following will cause compile-time errors:

    byte e = b + c;
    byte f = b + (byte) 2;
    byte g = 1 + 0;

    I hope that helps.

    --
    John Bollinger
    John C. Bollinger, Feb 14, 2005
    #5
  6. In article <>,
    (jk) wrote:

    > Hello: Is there an operator for directly adding bytes. I can do it
    > by creating instances of Byte, and getting their intValue, but is
    > there a direct method such as byte 'b = 2 + 3;'. I know that casting
    > this to bytes will work: 'b = (byte)2 + (byte)3;', but is there an
    > operator that does not require any conversion of types?
    >
    > Thanks in Advance
    > John
    >
    > =====================
    > Byte bA = new Byte((byte)65);
    > Byte b1 = new Byte((byte)1);
    > byte[] bytes = new byte[3];
    > bytes[0] = bA.byteValue();
    > bytes[1] = (byte)(bA.intValue() + b1.intValue()); // 66, B
    > ...
    > =====================


    This is becoming true at the machine layer too. Modern CPUs only
    operate on 32 or 64 bits. 8 bit math requires extra steps either in the
    microcode or the assembly code. The only advantage of 8 bit data is
    better RAM throughput in packed arrays.
    Kevin McMurtrie, Feb 15, 2005
    #6
  7. jk

    jk Guest

    I have been 'fiddling' with alternatives. I used the full adder as an
    inspiration. I came up with this code which on first simple tests
    seems to work. Any ideas and thoughts about this are welcome ;).

    for(int i = 0; i < 79; i++) {
    byte b = (byte)((48 | i) + (48 & i));
    byte[] bytes = {b};
    String s = new String(bytes);
    System.out.print(s);
    }
    System.out.println();

    /*
    program output
    0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    */

    Thanks in advance
    John, jk

    Kevin McMurtrie <> wrote in message news:<>...
    > In article <>,
    > (jk) wrote:
    >
    > > Hello: Is there an operator for directly adding bytes. I can do it
    > > by creating instances of Byte, and getting their intValue, but is
    > > there a direct method such as byte 'b = 2 + 3;'. I know that casting
    > > this to bytes will work: 'b = (byte)2 + (byte)3;', but is there an
    > > operator that does not require any conversion of types?
    > >
    > > Thanks in Advance
    > > John
    > >
    > > =====================
    > > Byte bA = new Byte((byte)65);
    > > Byte b1 = new Byte((byte)1);
    > > byte[] bytes = new byte[3];
    > > bytes[0] = bA.byteValue();
    > > bytes[1] = (byte)(bA.intValue() + b1.intValue()); // 66, B
    > > ...
    > > =====================

    >
    > This is becoming true at the machine layer too. Modern CPUs only
    > operate on 32 or 64 bits. 8 bit math requires extra steps either in the
    > microcode or the assembly code. The only advantage of 8 bit data is
    > better RAM throughput in packed arrays.
    jk, Feb 15, 2005
    #7
  8. jk wrote:

    > I have been 'fiddling' with alternatives. I used the full adder as an
    > inspiration. I came up with this code which on first simple tests
    > seems to work. Any ideas and thoughts about this are welcome ;).
    >
    > for(int i = 0; i < 79; i++) {
    > byte b = (byte)((48 | i) + (48 & i));
    > byte[] bytes = {b};
    > String s = new String(bytes);
    > System.out.print(s);
    > }
    > System.out.println();
    >
    > /*
    > program output
    > 0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    > */


    We already told you five times over that the only thing you need to
    worry about is casting the addition result to type byte when you perform
    the assignment. Were you somehow afraid that this wouldn't work? You
    can do the same thing with float and double results and it works there,
    too (though rounding and precision of floating-point values enters into
    the picture).

    --
    John Bollinger
    John C. Bollinger, Feb 15, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jiten

    about addition operator

    jiten, Jun 22, 2005, in forum: VHDL
    Replies:
    3
    Views:
    4,467
  2. Carson
    Replies:
    4
    Views:
    3,904
    Colin Marquardt
    Oct 6, 2005
  3. Ankit Aneja
    Replies:
    5
    Views:
    11,619
    Steven Cheng[MSFT]
    Apr 6, 2006
  4. Martin T.
    Replies:
    7
    Views:
    814
    Martin T.
    Mar 10, 2008
  5. Replies:
    6
    Views:
    151
    Bart Lateur
    Apr 30, 2006
Loading...

Share This Page