Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

S.O.S! HELP!

  Asked By: Adella    Date: Feb 07    Category: Java    Views: 440
  

I need your help please.

I'm programming something about bytes.

x[i] = x[i]+y[i];

x and y are byte arrays.

When I add two bytes, it says that it is possible to lose precision.

In my program, i don't care about the precision being lost!

How can I overcome this problem?

I'm looking forward to hearing from you as soon as possible.

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Hubert Taylor     Answered On: Feb 07

x[i] =new Integer(x[i]+y[i]).byteValue(); will work

 
Answer #2    Answered By: Lurlene Fischer     Answered On: Feb 07

When 2 bytes are added, the addition returns an int. Just cast the
addition back to a byte.

x[i] = (byte)(x[i]+y[i]);

 
Answer #3    Answered By: Helene Stewart     Answered On: Feb 07

A "byte" variable in Java can store numbers of 8 bits,
if you add two bytes you can obtain a number of 9 bits.
As you can see, a "byte" can not store that number.

I didn't see the program you are doing, but in general,
a simple solution is to use a "short" (16 bits) to store
the addition.

But, there are some special cases (on compression
algorithms as an example) where you can substract 256
if the addition is greater than 256, and take a
similar decision on the decompression process, but
like I said, that will depend on what your algorithm
does.

 
Didn't find what you were looking for? Find more on S.O.S! HELP! Or get search suggestion and latest updates.




Tagged: