public static class ex
{
public static byte[] RightShift(this byte[] ba, int n)
{
if (n < )
{
return ba.LeftShift(Math.Abs(n));
}
byte[] ba2 = null;
ba2 = ba.Clone() as byte[];
int loop = (int)Math.Ceiling(n / 8.0);
byte tempByte = ;
byte tempByte2 = ;
byte Header = ; for (int i = ; i < loop; i++)
{
var tempN = i + == loop ? n % : ;
if (tempN == && n != )
{
tempN = ;
}
for (int j = ; j < ba.Length; j++)
{
if (j == )
{
Header = (byte)((ba2.First() & ((byte)(Math.Pow(, tempN) - ))) << ( - tempN));
tempByte = (byte)((ba2[ba.Length - - j] & ((byte)(Math.Pow(, tempN) - ))) << ( - tempN));
ba2[ba.Length - - j] >>= tempN;
}
else
{
tempByte2 = (byte)((ba2[ba.Length - - j] & ((byte)(Math.Pow(, tempN) - ))) << ( - tempN));
ba2[ba.Length - - j] >>= tempN;
ba2[ba.Length - - j] |= tempByte;
tempByte = tempByte2;
if (j + == ba.Length)
{
ba2[j] |= Header;
}
}
}
}
return ba2;
}
public static byte[] LeftShift(this byte[] ba, int n)
{
if (n < )
{
return ba.RightShift(Math.Abs(n));
}
byte[] ba2 = null;
ba2 = ba.Clone() as byte[];
int loop = (int)Math.Ceiling(n / 8.0);
byte tempByte = ;
byte tempByte2 = ;
byte Header = ; for (int i = ; i < loop; i++)
{
var tempN = i + == loop ? n % : ;
if (tempN == && n != )
{
tempN = ;
}
for (int j = ; j < ba.Length; j++)
{
if (j == )
{
Header = (byte)(ba2.Last() & ((byte)(Math.Pow(, tempN) - ) << ( - tempN)));
tempByte = (byte)(ba2[j] & ((byte)(Math.Pow(, tempN) - ) << ( - tempN)));
ba2[j] <<= tempN;
}
else
{
tempByte2 = (byte)(ba2[j] & ((byte)(Math.Pow(, tempN) - ) << ( - tempN)));
ba2[j] <<= tempN;
ba2[j] |= (byte)(tempByte >> ( - tempN));
tempByte = tempByte2;
if (j + == ba.Length)
{
ba2[] |= (byte)(Header >> ( - tempN));
}
}
}
}
return ba2;
}
public static byte[] BitAnd(this byte[] ba1, byte[] ba2)
{
if (ba1.Length != ba2.Length)
{
return new byte[];
}
var ba3 = new byte[ba1.Length];
for (int i = ; 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[];
}
var ba3 = new byte[ba1.Length];
for (int i = ; i < ba3.Length; i++)
{
ba3[i] = (byte)((byte)ba1[i] | (byte)ba2[i]);
}
return ba3; }
}

byte[] 左移和右移的更多相关文章

  1. 【转】C语言位运算符:与、或、异或、取反、左移与右移详细介绍

    转载自:http://www.jb51.net/article/40559.htm,感谢原作者. 以下是对C语言中的位运算符:与.或.异或.取反.左移与右移进行了详细的分析介绍,需要的朋友可以过来参考 ...

  2. C语言位运算符:与、或、异或、取反,左移和右移

    C语言位运算符:与.或.异或.取反.左移和右移 个位操作运算符.这些运算符只能用于整型操作数,即只能用于带符号或无符号的char,short,int与long类型. ,则该位的结果值为1,否则为0 | ...

  3. C语言位运算符:与、或、异或、取反、左移和右移

    语言位运算符:与.或.异或.取反.左移和右移 位运算是指按二进制进行的运算.在系统软件中,常常需要处理二进制位的问题.C语言提供了6个位操作运算符.这些运算符只能用于整型操作数,即只能用于带符号或无符 ...

  4. 分析轮子(二)- << ,>>,>> (左移、右移、无符号右移)

    前言:写 分析轮子(一)-ArrayList.java 的时候看到源码中有 int newCapacity = oldCapacity + (oldCapacity >> 1); 这样的代 ...

  5. [java基础] java 左移和右移

    今天搜到一个比较好用的在线编译器,希望和大家分享. 除了java还有c++....,地址是http://www.tutorialspoint.com/compile_java_online.php 另 ...

  6. C++中的左移、右移运算

    移位运算包含“逻辑移位”(logical shift)和“算术移位”(arithmetic shift). 逻辑移位:移出去的位丢弃,空缺位(vacant bit)用 0 填充. 算术移位:移出去的位 ...

  7. C# ListBox 左移、右移、上移、下移

    C# ListBox 左移.右移.上移.下移 2012-11-17 22:53:45|  分类: 技术研讨 |  标签:listbox  |字号 订阅     /// <summary>  ...

  8. WPF 小矢量图 : 主页,返回,加,减,文字按钮,左移,右移

    原文:WPF 小矢量图 : 主页,返回,加,减,文字按钮,左移,右移 代码:: <UserControl x:Class="SQ.TestPage" xmlns=" ...

  9. C语言之左移和右移运算符

    C语言中的左移和右移运算符移位后的结果老是忘记,最近在刷有关位操作的题目,正好整理下:   1. 左移运算符(<<) 左移运算符是用来将一个数的各二进制位左移若干位,移动的位数由右操作数指 ...

随机推荐

  1. C#生成、解析xml文件以及处理报错原因

    转载自:http://blog.csdn.net/lilinoscar/article/details/21027319 简单的介绍一下生成XML文件以及解析,因为有些数据不一定放到数据库,减少链接数 ...

  2. HttpServletRequest方法

    HttpServletRequest HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,开发人员通过这个对象 ...

  3. 2016.3.14__CSS 定位__第六天

    假设您认为这篇文章还不错.能够去H5专题介绍中查看很多其它相关文章. CSS 定位机制 CSS中一共同拥有三种基本定位机制:普通流.浮动.绝对定位. 假设不进行专门指定.全部的标签都在普通流中定位. ...

  4. [Flow] More tips about Flow

  5. [Angular] USING ZONES IN ANGULAR FOR BETTER PERFORMANCE

    Link to the artical. Zone detects any async opreations. Once an async oprations happens in Angular, ...

  6. [Angular] Wrap a third party lib into service

  7. 公布一个基于CSDN Code的学习測试仓库

    使用CSDN Code代码托管平台有一段时间了,今天新建立了一个公开的仓库https://code.csdn.net/smstong/learngit/tree/master,供大家測试合并请求等协作 ...

  8. Eclipse 一直不停 building workspace... 完美解决总结

    Eclipse 一直不停 building workspace... 一.产生这个问题的原因多种 1.自动升级 2.未正确关闭  3.maven下载lib挂起 等..二.解决总结 (1).解决方法  ...

  9. POJ 1985 - 树的直径

    传送门 题目大意 给一颗n个点的树,求树的直径(最长的一条链) 题解 先随便找一个点u,dfs出离它最远的点v 于是有以下情况: 直径就是这条链 直径经过u,是这条链的延长 直径不经过u 只需要从v再 ...

  10. hive 分区表与数据产生关联的三种方式

    所谓关联,可以理解为能够使用select查询到 1.load 这是最常用的一种方式 load data [local] inpath "数据路径" into table table ...