• byte[] 左移和右移


        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;
    
            }
        }
  • 相关阅读:
    SpringCloud之架构搭建
    3.通用权限设计——SnailAspNetCoreFramework快速开发框架之后端设计
    2.接口输入校验、输出格式、及异常处理——SnailAspNetCoreFramework快速开发框架之后端设计
    1、框架内各项目及目录的介绍和总设计思路——SnailAspNetCoreFramework快速开发框架
    SnailAspNetCoreFramework框架系列博客
    Asp.net core中间件实现原理及用法解说
    深入剖析linq的联接
    webapi框架搭建系列博客
    webapi框架搭建-依赖注入之autofac
    webapi框架搭建-创建项目(三)-webapi owin
  • 原文地址:https://www.cnblogs.com/nocanstillbb/p/10573776.html
Copyright © 2020-2023  润新知