byte type and addition operator

J

jk

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

Eric Sosman

jk said:
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.)
 
K

Knute Johnson

jk said:
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);
 
J

John C. Bollinger

jk said:
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.
 
K

Kevin McMurtrie

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

jk

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 said:
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.
 
J

John C. Bollinger

jk said:
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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top