0

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.

7
  • 2
    Why not just use longs instead of arrays of 8 bytes? Commented Jun 29, 2017 at 0:09
  • Or convert the byte array to long, do the add, and convert it back, if you really must have these byte arrays. Commented Jun 29, 2017 at 0:11
  • I have encoded data which is stored in byte arrays like above. Is there a way to convert to long, do the operation and then convert back? Commented Jun 29, 2017 at 0:13
  • The final byte in the result should be 64. Commented Jun 29, 2017 at 0:21
  • @EJP Not sure how you're getting those numbers. Commented Jun 29, 2017 at 0:42

2 Answers 2

3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

2

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.

4 Comments

Doesn't this method needs to have two arguments passed to it?
Two arguments such as what? It's your method signature. I didn't change it.
My method is for incrementing a single byte array. I need a method to add together two byte arrays.
Well your incrementing method doesn't work either, that's what I answered originally. Otherwise I have no idea why you posted it. I've now added the add method.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.