Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > byte type and addition operator

Reply
Thread Tools

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?

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
....
=====================
 
Reply With Quote
 
 
 
 
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.
 
Reply With Quote
 
 
 
 
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)

 
Reply With Quote
 
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?
>
> 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/
 
Reply With Quote
 
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)
 
Reply With Quote
 
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?
>
> 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.
 
Reply With Quote
 
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
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 <(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?
> >
> > 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.

 
Reply With Quote
 
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
> 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
(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is a "convert to any type" operator overkill and dangerous? template<typename T> operator T () Qi C++ 2 07-01-2011 03:39 AM
std::operator<<(basic_ostream&, type) vs. std::basic_ostream::operator(type)... ? Martin T. C++ 7 03-10-2008 10:42 AM
perl addition operator. query. gerry.brennan@gmail.com Perl Misc 6 04-30-2006 08:54 AM
a simple addition "+" operator question Carson VHDL 4 10-06-2005 09:01 PM
about addition operator jiten VHDL 3 06-23-2005 05:26 AM



Advertisments