0

I have two 2D byte arrays that I want to merge. For example:

2D array1:         2D array2:           Combined array3:
[1][0][1]          [0][1][0]            [1][1][1]
[0][0][0]          [1][0][1]     =>     [1][0][1]
[1][0][1]          [0][1][0]            [1][1][1]

I tried like this:

for(int i = 0; i < array3.GetLength(0); i++)
{
    for(int j = 0; j < array3.GetLength(1); j++)
    {
        // Missing a cast
        array3[i, j] = array1[i, j] + array2[i, j];
    }
}

The line under the comment says it cannot convert to byte from int. I was reading this: byte + byte = int... why?, but still couldn't figure out what I want to accomplish.

How would I go about combining them? Thanks in advance!

Community
  • 1
  • 1
o.o
  • 3,563
  • 9
  • 42
  • 71
  • 2
    `array3[i, j] = (byte)(array1[i, j] + array2[i, j]);` Its addition - not sure where/how `filling in zeroes` comes into play – Ňɏssa Pøngjǣrdenlarp Aug 03 '16 at 00:36
  • Weird, I tried that but didn't work. Restarted Visual Studio and now it works. By filling zeroes, I meant replacing all the zeroes where there is a 1 in the other array. Thanks for the help! – o.o Aug 03 '16 at 00:40

1 Answers1

1

Try this to get the output you want:

for(int i = 0; i < array3.GetLength(0); i++)
{
    for(int j = 0; j < array3.GetLength(1); j++)
    {
        // All you need to do is cast the int to a byte.
        array3[i, j] = (byte)((array1[i, j] == 0) ? array2[i, j] : array1[i, j]);
    }
}

Adding array1[i, j] and array2[i, j] could give you 2, which is not wat you want in your example.