byte[] 左移和右移
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[] 左移和右移的更多相关文章
- 【转】C语言位运算符:与、或、异或、取反、左移与右移详细介绍
转载自:http://www.jb51.net/article/40559.htm,感谢原作者. 以下是对C语言中的位运算符:与.或.异或.取反.左移与右移进行了详细的分析介绍,需要的朋友可以过来参考 ...
- C语言位运算符:与、或、异或、取反,左移和右移
C语言位运算符:与.或.异或.取反.左移和右移 个位操作运算符.这些运算符只能用于整型操作数,即只能用于带符号或无符号的char,short,int与long类型. ,则该位的结果值为1,否则为0 | ...
- C语言位运算符:与、或、异或、取反、左移和右移
语言位运算符:与.或.异或.取反.左移和右移 位运算是指按二进制进行的运算.在系统软件中,常常需要处理二进制位的问题.C语言提供了6个位操作运算符.这些运算符只能用于整型操作数,即只能用于带符号或无符 ...
- 分析轮子(二)- << ,>>,>> (左移、右移、无符号右移)
前言:写 分析轮子(一)-ArrayList.java 的时候看到源码中有 int newCapacity = oldCapacity + (oldCapacity >> 1); 这样的代 ...
- [java基础] java 左移和右移
今天搜到一个比较好用的在线编译器,希望和大家分享. 除了java还有c++....,地址是http://www.tutorialspoint.com/compile_java_online.php 另 ...
- C++中的左移、右移运算
移位运算包含“逻辑移位”(logical shift)和“算术移位”(arithmetic shift). 逻辑移位:移出去的位丢弃,空缺位(vacant bit)用 0 填充. 算术移位:移出去的位 ...
- C# ListBox 左移、右移、上移、下移
C# ListBox 左移.右移.上移.下移 2012-11-17 22:53:45| 分类: 技术研讨 | 标签:listbox |字号 订阅 /// <summary> ...
- WPF 小矢量图 : 主页,返回,加,减,文字按钮,左移,右移
原文:WPF 小矢量图 : 主页,返回,加,减,文字按钮,左移,右移 代码:: <UserControl x:Class="SQ.TestPage" xmlns=" ...
- C语言之左移和右移运算符
C语言中的左移和右移运算符移位后的结果老是忘记,最近在刷有关位操作的题目,正好整理下: 1. 左移运算符(<<) 左移运算符是用来将一个数的各二进制位左移若干位,移动的位数由右操作数指 ...
随机推荐
- style.height、offsetHeight、clientHeight、scrollHeight的差别
style.height 包含元素的滚动栏,不包含边框 clientHeight 不包含元素的滚动栏和边框 offsetHeight 包含元素的滚动栏和边框 scrollHeight offsetHe ...
- [_UICascadingTextStorage attributesAtIndex:effectiveRange:]: Range or index out of bounds
之前写过一篇<如何更好地限制一个UITextField的输入长度>,在文章最后得到的结论是可以直接使用 UIKIT_EXTERN NSString *const UITextFieldTe ...
- OpenGL学习一
作者:朱金灿 来源:http://blog.csdn.net/clever101 回家计划学习OpenGL开发.没有开发机子,用的是别人的笔记本,不想装庞大的VS,于是选择小巧一点的codeblock ...
- 从张量积(tensor product)到多重线性代数(multilinear algebra)
记张量积的数学记号为 ⊗. 1. linear 假设 V,W 为线性空间(vector spaces),f:V→W是线性(linear)的,如果满足: f(v1+v2)=f(v1)+f(v2)f(αv ...
- WPF入门(三)->两个几何图形合并(CombinedGeometry)
原文:WPF入门(三)->两个几何图形合并(CombinedGeometry) 在WPF中,提供了一个CombinedGeometry对象可以使两个几何图形合并产生效果 CombinedGeom ...
- 【BZOJ 1014】 [JSOI2008]火星人prefix
[题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1014 [题意] 让你在线查询最长公共前缀. 支持单节点修改; 插入操作; [题解] / ...
- jQuery实现复选框的全选、反选、并且根据复选框的<checked属性>控制多个对应div的显示/隐藏
<!doctype html><html> <head> <meta charset="utf-8"> <title>j ...
- 【BZOJ 1026】 [SCOI2009]windy数
[题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1026 [题意] [题解] 数位Dp 设f[i][j]表示长度为i,第一位(也就是最高位 ...
- 80. Domino Internet Password
Internet口令保存在Domino文件夹的个人文档的HTTPPassword域中,和文档中的username一起用于藉各种Internet协议訪问Dominoserver时的校验,最经常使用的就是 ...
- FreeBSD 内核中的SYSINIT分析【转】
FreeBSD kernel是一个膨大的系统, 对于这样一个大系统, 里面往往包含了大量的子系统和 模块,当系统初始化时这些模块就需要初始化, 按照通常的思路,这些初始化过程必须在某处 被显式地调 ...