Ali
Ali

Reputation: 859

How to add two 8 byte numbers stored in byte[] array

If the byte value increase as 0-127, then -128 to 0 and then the next significant bit is incremented. How can I add two 8 byte numbers?

5824 samples =  0 0 0 0 0 0 22 -64
6272 samples =  0 0 0 0 0 0 24 -128
-----------------------------------
12096 samples = 0 0 0 0 0 0 47 64

5824 samples + 6272 samples = 12096 samples

I am incrementing the byte array using this code:

public static byte[] byteIncrement(byte[] array) {
    byte[] r = array.clone();
    for ( int i = array.length - 1; i >= 0; i-- ) { // LSB to MSB
        byte x = array[ i ];
        if ( x == -1 )
            continue;
        r[ i ] = (byte) (x + 1);
        Arrays.fill( r, i + 1, array.length, (byte) 0 );
        return r;
    }
    throw new IllegalArgumentException( Arrays.toString( array ) );
}

For example when I increment this byte array 5284 times I get value given above of 5824 samples:

 byte[] array = {(byte) (0), (byte) (0), (byte) (0), (byte) (0), (byte) (0), (byte) (0), (byte) (0), (byte) (0)};
 byte[] incrementedarray = byteIncrement(array);
 for(int k=1;k<=5823;k++) { // 5824 samples for 1st page
        incrementedarray = byteIncrement(incrementedarray);
 }

So my question is, how to add such 8 byte (64 bit) numbers in the manner described above. I am using byte arrays because the granule position in an OGG audio bit stream is stored in this way. I am trying to combine two bit streams for which I have to do such addition.

Upvotes: 0

Views: 796

Answers (2)

user207421
user207421

Reputation: 310876

Your code makes no sense whatsoever. If you must do it longhand, you need to deal with the carry:

public static byte[] byteIncrement(byte[] array) {
    byte[] r = array.clone();
    int carry = 1; // as we are incrementing
    for ( int i = array.length - 1; i >= 0; i-- ) { // LSB to MSB
        int sum = (array[i] & 0xff)+carry;
        r[i] = (byte) sum;
        carry = sum >>> 8;
    }
    return r;
}

If you want to add two of these byte arrays together:

public static byte[] add(byte[] array1, byte[] array2) {
    byte[] r = new byte[array1.length]; // assuming both arrays are same length
    int carry = 0;
    for ( int i = array.length - 1; i >= 0; i-- ) { // LSB to MSB
        int sum = (array1[i] & 0xff)+(array2[i] & 0xff)+carry;
        r[i] = (byte) sum;
        carry = sum >>> 8;
    }
    return r;
}

Errors & omissions excepted.

Upvotes: 2

shmosel
shmosel

Reputation: 50716

You can use BigInteger to handle the math:

public static byte[] add(byte[] arr1, byte[] arr2) {
    return new BigInteger(arr1).add(new BigInteger(arr2)).toByteArray();
}

If the result needs to be a specific size, you can copy it to a new array with the target size:

private static byte[] leftPad(byte[] arr, int size) {
    if (arr.length < size) {
        byte[] padded = new byte[size];
        System.arraycopy(arr, 0, padded, size - arr.length, arr.length);
        return padded;
    }
    return arr;
}

Upvotes: 3

Related Questions