# Java Basics Help

if i have a following code

int sum = 433;
byte[] checksum = {(byte)(sum / 256) ,(byte)(sum % 256) };

System.out.println("Higher order byte = " + (byte)(sum / 256));
System.out.println("Lower order byte = " + (byte)(sum % 256));

when i print it i have a negative value for the lower order byte

but i like to have a positive value
how could i acheive it

with regards,

Ashok.G
LVL 5
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Author Commented:
I have a value for Higher order byte as 1

value for Lower order byte = -79

but i dont want to have -79

but i would like to have a remainder of the sum%256

how can i do it
0
Software engineerCommented:
System.out.println("Lower order byte = " + (int)(sum % 256));

prints: 177
0
Commented:
(sum%256+256)%256
0
Software engineerCommented:
The max value a byte can have is 2^7-1 = 127
0
Author Commented:
but i want to add in to a byte array checksum
0
Software engineerCommented:
Since,
>>The max value a byte can have is 2^7-1 = 127
The min value a byte can have is -2^7 = -128
it will be difficult to add 177 in that array (while -97 is possible)
0
Software engineerCommented:
This is how you get your original number back:

int sum = 433;
byte[] checksum = {(byte)(sum / 256) ,(byte)(sum % 256) };

System.out.println("Higher order byte = " + (byte)(sum / 256));
System.out.println("Lower order byte = " + (int)(sum % 256));
int result = (int) (256*checksum[0]);
result += (int)(256+checksum[1]);
System.out.println( result );
0
Author Commented:
I need to some how add the byte checksum with no negative values to the array temp3.

how could i acheive it.

byte[] checksum = {(byte)(sum / 256) ,(byte)(sum % 256 ) };
byte[] temp3 = new byte[ identify.length + checksum.length] ;

System.arraycopy( identify, 0, temp3, 0, identify.length ) ;
System.arraycopy(checksum, 0, temp3, identify.length, checksum.length ) ;
identify = temp3 ;

for (int i = 0; i < identify.length; i++)
{
System.out.println("Elements at " + i + " = " + identify[i]);
}

With regards,

Ashok.G
0
Software engineerCommented:
Did you missed my previous post?

>>The max value a byte can have is 2^7-1 = 127
>>The min value a byte can have is -2^7 = -128
So, it is *impossible* to add the value 177 into the byte array!
0
Commented:
You can also use bit masking to get the low/hi order bytes:
byte[] checksum = {(byte)(sum & 0x00FF) ,(byte)((sum & 0xFF00) >>>8) };
(the >>> is an unsigned right shift)

The two values will be positive values as long as the sum wasn't greater than 2^15-1 (32767).
0
Software engineerCommented:
>> The two values will be positive values as long as the sum wasn't greater than 2^15-1 (32767).
Sure of that?

sum = 433;      in hex: 01B1                   8 7 6 5  4 3 2 1    8 7 6 5  4 3 2 1
0 0 0 0  0 0 0 1    1 0 1 1  0 0 0 1

bitwise & with    0x00FF                         0  0 0 0  0 0 0 0   1 1 1 1  1 1 1 1   = 0x00B1 = 177 decimal

How would you put 177 in a byte?

checksum[0] = -79
0
Software engineerCommented:
ashok3sep, a correction:

int sum = 433; // or 233 to test the "else" path of the added if statement

byte[] checksum = {(byte)(sum / 256) ,(byte)(sum % 256) };

System.out.println("Higher order byte = " + (byte)(sum / 256));
System.out.println("Lower order byte = " + (int)(sum % 256));
int result = (int) (256*checksum[0]);
if (checksum[1]<0)                                  // <<<<<< condition added
result += (int)(256+checksum[1]);
else
result += (int)(checksum[1]);
System.out.println( result );
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Software engineerCommented:
Hi ashok, any news?
0
Software engineerCommented:
Thanks
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.