Velocity Reviews > Java > byte type and addition operator

# byte type and addition operator

jk
Guest
Posts: n/a

 02-14-2005
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?

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
....
=====================

Tor Iver Wilhelmsen
Guest
Posts: n/a

 02-14-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) (jk) writes:

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

No.

Eric Sosman
Guest
Posts: n/a

 02-14-2005

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.)

--
(E-Mail Removed)

Knute Johnson
Guest
Posts: n/a

 02-14-2005
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?
>
> 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/

John C. Bollinger
Guest
Posts: n/a

 02-14-2005
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
(E-Mail Removed)

Kevin McMurtrie
Guest
Posts: n/a

 02-15-2005
In article <(E-Mail Removed) >,
(E-Mail Removed) (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?
>
> 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
Guest
Posts: n/a

 02-15-2005
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

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{|}~
*/

John, jk

Kevin McMurtrie <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> In article <(E-Mail Removed) >,
> (E-Mail Removed) (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?
> >
> > 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.

John C. Bollinger
Guest
Posts: n/a

 02-15-2005
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
>
> 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
(E-Mail Removed)