简体   繁体   中英

Adding two little endian byte array

I have two byte array in little endian that I need to add. Obviosly this code doesn't work.

for (int i=0;i<bytes1.getB1().length;i++){
    bAdded[i]=(byte) (bytes1[i]+bytes2[i]);
}

How can I add these two little endian bytes array ?

What you mean by "doesn't work"?

If you mean that it isn't performing carrying between bytes, here's how you could do it:

int carry = 0;

for (int i = 0; i < bytes1.getB1().length; i++) {
    int sum = bytes1[i] + bytes2[i] + carry;
    bAdded[i] = (byte) sum;
    carry = sum >> 8;
}

bAdded[bytes1.getB1().length] = carry;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM