public static void Main()
Random rand = new Random();
for (int i = 0; i < n; i++)
f[i] = (byte)rand.Next(0, 256);
s[i] = (byte)rand.Next(0, 256);
byte[] result = AddRecursive(f, s);
result.Print("Total result");
public static byte[] AddRecursive(byte[] f, byte[] s)
Summator summator = new Summator(f, s);
public Summator(byte[] f, byte[] s)
_result = new byte[_f.Length];
return new byte[] { 1 }.Concat(_result);
private bool AddBytesByIndex(int startIndex)
if (startIndex < _f.Length - 1)
carryOver = AddBytesByIndex(startIndex + 1) ? 1 : 0;
int sum = _f[startIndex] + _s[startIndex] + carryOver;
bool isOverflow = sum > 255;
_result[startIndex] = (byte)(sum - 256);
_result[startIndex] = (byte)sum;
public static class ArrayExtensions
public static byte[] Slice(this byte[] array, int startIndex)
if (startIndex > array.Length)
throw new ArgumentException("Invalid startIndex");
byte[] result = new byte[array.Length - startIndex];
Array.Copy(array, startIndex, result, 0, result.Length);
public static byte[] Concat(this byte[] first, byte[] second)
byte[] result = new byte[first.Length + second.Length];
second.CopyTo(result, first.Length);
public static void Print(this byte[] array, string arrayName)
Console.WriteLine(arrayName + ": [" + String.Join(", ", array) + "]");