public static class ex
    {
        public static byte[] RightShift(this byte[] ba, int n)
        {
            if (n < 0)
            {
                return ba.LeftShift(Math.Abs(n));
            }
            byte[] ba2 = null;
            ba2 = ba.Clone() as byte[];
            int loop = (int)Math.Ceiling(n / 8.0);
            byte tempByte = 0;
            byte tempByte2 = 0;
            byte Header = 0;

            for (int i = 0; i < loop; i++)
            {
                var tempN = i + 1 == loop ? n % 8 : 8;
                if (tempN == 0 && n != 0)
                {
                    tempN = 8;
                }
                for (int j = 0; j < ba.Length; j++)
                {
                    if (j == 0)
                    {
                        Header = (byte)((ba2.First() & ((byte)(Math.Pow(2, tempN) - 1))) << (8 - tempN));
                        tempByte = (byte)((ba2[ba.Length - 1 - j] & ((byte)(Math.Pow(2, tempN) - 1))) << (8 - tempN));
                        ba2[ba.Length - 1 - j] >>= tempN;
                    }
                    else
                    {
                        tempByte2 = (byte)((ba2[ba.Length - 1 - j] & ((byte)(Math.Pow(2, tempN) - 1))) << (8 - tempN));
                        ba2[ba.Length - 1 - j] >>= tempN;
                        ba2[ba.Length - 1 - j] |= tempByte;
                        tempByte = tempByte2;
                        if (j + 1 == ba.Length)
                        {
                            ba2[j] |= Header;
                        }
                    }
                }
            }
            return ba2;
        }
        public static byte[] LeftShift(this byte[] ba, int n)
        {
            if (n < 0)
            {
                return ba.RightShift(Math.Abs(n));
            }
            byte[] ba2 = null;
            ba2 = ba.Clone() as byte[];
            int loop = (int)Math.Ceiling(n / 8.0);
            byte tempByte = 0;
            byte tempByte2 = 0;
            byte Header = 0;

            for (int i = 0; i < loop; i++)
            {
                var tempN = i + 1 == loop ? n % 8 : 8;
                if (tempN == 0 && n != 0)
                {
                    tempN = 8;
                }
                for (int j = 0; j < ba.Length; j++)
                {
                    if (j == 0)
                    {
                        Header = (byte)(ba2.Last() & ((byte)(Math.Pow(2, tempN) - 1) << (8 - tempN)));
                        tempByte = (byte)(ba2[j] & ((byte)(Math.Pow(2, tempN) - 1) << (8 - tempN)));
                        ba2[j] <<= tempN;
                    }
                    else
                    {
                        tempByte2 = (byte)(ba2[j] & ((byte)(Math.Pow(2, tempN) - 1) << (8 - tempN)));
                        ba2[j] <<= tempN;
                        ba2[j] |= (byte)(tempByte >> (8 - tempN));
                        tempByte = tempByte2;
                        if (j + 1 == ba.Length)
                        {
                            ba2[0] |= (byte)(Header >> (8 - tempN));
                        }
                    }
                }
            }
            return ba2;
        }
        public static byte[] BitAnd(this byte[] ba1, byte[] ba2)
        {
            if (ba1.Length != ba2.Length)
            {
                return new byte[0];
            }
            var ba3 = new byte[ba1.Length];
            for (int i = 0; i < ba3.Length; i++)
            {
                ba3[i] = (byte)((byte)ba1[i] & (byte)ba2[i]);
            }
            return ba3;

        }
        public static byte[] BitOR(this byte[] ba1, byte[] ba2)
        {
            if (ba1.Length != ba2.Length)
            {
                return new byte[0];
            }
            var ba3 = new byte[ba1.Length];
            for (int i = 0; i < ba3.Length; i++)
            {
                ba3[i] = (byte)((byte)ba1[i] | (byte)ba2[i]);
            }
            return ba3;

        }
    }

 

相关文章:

  • 2022-02-09
  • 2022-12-23
  • 2021-08-02
  • 2021-11-20
  • 2022-02-09
  • 2022-02-09
  • 2022-02-09
  • 2022-02-09
猜你喜欢
  • 2022-02-09
  • 2021-07-05
  • 2022-12-23
  • 2022-02-09
  • 2022-12-23
  • 2021-11-29
相关资源
相似解决方案