System.Net.Topology.ByteArrayExtensions.Or C# (CSharp) Method

Or() static private method

static private Or ( this b1, byte b2 ) : byte[]
b1 this
b2 byte
return byte[]
        internal static byte[] Or(this byte[] b1, byte[] b2)
        {
            if (b1 == null)
                throw new ArgumentNullException(nameof(b1));
            if (b2 == null)
                throw new ArgumentNullException(nameof(b2));

            if (b1.Length == 1 && b2.Length == 1)
            {
                int ib1 = b1[0];
                int ib2 = b2[0];
                return new[] { (byte)(ib1 | ib2) };
            }
            if (b1.Length == 2 && b2.Length == 2)
            {
                var sb1 = BitConverter.ToInt16(b1, 0);
                var sb2 = BitConverter.ToInt16(b2, 0);
                return BitConverter.GetBytes((short)(sb1 | sb2));
            }
            if (b1.Length == 4 && b2.Length == 4)
            {
                var ib1 = BitConverter.ToInt32(b1, 0);
                var ib2 = BitConverter.ToInt32(b2, 0);
                return BitConverter.GetBytes(ib1 | ib2);
            }
            if (b1.Length != b2.Length)
            {
                // Or maybe throw exception?

                int maxIndex = Math.Max(b1.Length, b2.Length);
                byte[] biggerArray = b1.Length > b2.Length ? b1 : b2;
                byte[] smallerArray = b1.Length <= b2.Length ? b1 : b2;

                var paddedArray = new byte[maxIndex];

                Buffer.BlockCopy(smallerArray, 0, paddedArray, 0, smallerArray.Length);

                Debug.Assert(biggerArray.Length == paddedArray.Length);

                return Or(biggerArray, paddedArray);
            }

            var targetIndex = b1.Length;
            var oredArray = new byte[targetIndex];
            Buffer.BlockCopy(b1, 0, oredArray, 0, oredArray.Length);

            for (int i = 0; i < targetIndex; ++i)
                oredArray[i] |= b2[i];

            return oredArray;
        }