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. 左移运算符(<<) 左移运算符是用来将一个数的各二进制位左移若干位,移动的位数由右操作数指 ...
随机推荐
- RSA解密时javax.crypto.BadPaddingException: Data must start with zero
解决方法:要在加密后产生的byte数组转成string时要在各byte之间加个标识符,我加了个空格,然后再根据空格分隔转换回byte数组.如果不加标识符,由于byte值可能是一位到三位,无法知道某一个 ...
- [Angular] Using the platform agnostic Renderer & ElementRef
ElementRef: ElementRef is a way to access native html element, notice that it only works for Broswer ...
- erlang抽象码与basho的protobuf
erlang抽象码与basho的protobuf(一)使用 erlang抽象码与basho的protobuf(二)代码生成原理之词法与语法分析 erlang抽象码与basho的protobuf(三)代 ...
- RPC与REST的差别
一:RPC RPC 即远程过程调用, 非常easy的概念, 像调用本地服务(方法)一样调用server的服务(方法). 通常的实现有 XML-RPC , JSON-RPC , 通信方式基本同样, 所不 ...
- Java循环输出一个菱形与阶乘倒数
package javafirst; public class HomeWork { public static void main(String[] args){ System.out.printl ...
- Linux硬件信息查询命令
系统 uname -a # 查看内核/操作系统/CPU信息 Linux hostname 2.6.18-128.el5 #1 SMP Wed Dec 17 11:41:38 ...
- 商(quotient)—— 两数之比
1. IQ:Intelligence quotient IQ=MACA×100 MA:心智年龄(mental age) CA:实足年龄(chronological age)
- In partitioned databases, trading some consistency for availability can lead to dramatic improvements in scalability.
In partitioned databases, trading some consistency for availability can lead to dramatic improvement ...
- phpcms视图查询数据
{pc:get sql="SELECT * FROM phpcms WHERE id in ($id) ORDER BY listorder ASC LIMIT 0, 1--"re ...
- zabbix 设备(自己的实践)
1. 下载源代码包 wget http://sourceforge.net/projects/zabbix/files/ 2. 解压 tar -zxvf zabbix-2.2.3.tar.gz 3. ...