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. 左移运算符(<<) 左移运算符是用来将一个数的各二进制位左移若干位,移动的位数由右操作数指 ...
随机推荐
- Linux文件编辑命令具体整理
刚接触Linux,前几天申请了个免费体验的阿里云server,选择的是Ubuntu系统.配置jdk环境变量的时候须要编辑文件. vi命令编辑文件,百度了一下,非常多回答不是非常全面,因此编辑文件话了一 ...
- js进阶 11-14 jquery如何实现元素的替换和遍历
js进阶 11-14 jquery如何实现元素的替换和遍历 一.总结 一句话总结:替换:replaceAll() 与 replaceWith().遍历:each(). 1.replaceAll() ...
- Django之模板过滤器
Django 模板过滤器也是我们在以后基于 Django 网站开发过程中会经常遇到的,如显示格式的转换.判断处理等.以下是 Django 过滤器列表,希望对为大家的开发带来一些方便. 一.形式:小写 ...
- php中如何获取数组长度
php获取数组的长度的方法 一.总结 一句话总结:count方法和sizeof方法 二.php获取数组的长度的方法 php获取数组长度的方法: 一). 获取一维数组的方法: 1.count.sizeo ...
- Ehcache配置
http://blog.csdn.net/lwx2615/article/details/5624388 http://www.cnblogs.com/hoojo/archive/2012/07/12 ...
- [Ramda] Get Deeply Nested Properties Safely with Ramda's path and pathOr Functions
In this lesson we'll see how Ramda's path and pathOr functions can be used to safely access a deeply ...
- CoreLocation定位
nCoreLocation n简介 n在移动互联网时代,移动app能解决用户的很多生活琐事,比如 p导航:去任意陌生的地方 p周边:找餐馆.找酒店.找银行.找电影院 p n在上述应用中,都用到了地 ...
- 【t016】邮递员
Time Limit: 1 second Memory Limit: 32 MB [问题描述] 邮局需要你来帮助他们为某个邮递员设计出一条能够穿过那遥远乡村的所有村子和小路至少一次的邮路(输入数据将会 ...
- TensorFlow 学习(八)—— 梯度计算(gradient computation)
maxpooling 的 max 函数关于某变量的偏导也是分段的,关于它就是 1,不关于它就是 0: BP 是反向传播求关于参数的偏导,SGD 则是梯度更新,是优化算法: 1. 一个实例 relu = ...
- [Android]对话框样式Activity获得窗口外点击事件
Dialog除了使用Dialog类来实现之外,还可以使用Dialog样式的Activity来实现,只需要在注册Activity时指明theme为adnroid:Theme.Dialog就行,这样的Di ...