C#代码解释。。。高手〉〉〉

来源:百度知道 编辑:UC知道 时间:2024/06/05 14:35:02
sing System;
using System.Collections;

class BitArr

{
int[] bits;
int length;
public BitArr(int length)
{
if(length<0)
throw new ArgumentException();
bits = new int[((length - 1 >> 5) + 1)];
this.length = length;
}
public bool this[int idx]
{
get
{
if(idx<0 || idx>=length)
throw new IndexOutOfRangeException();
return (bits[idx >> 5] & 1 << idx) != 0;
}
set
{
if (idx < 0 || idx >= length)
throw new IndexOutOfRangeException();
if (value)
bits[idx >> 5] |= 1 << idx;
}
}
}

要解释的语句1:

return (bits[idx >> 5] & 1 << idx) != 0;

要解释的语

bits[idx >> 5] |= 1 << idx;

这句话的难点在 ">>" 这个运算符号

1. >> 是位移运算符号 右移一位相当于除以2,右移n位相当于除以2n
LZ 的 是右移 5 就是说 ??? 自己算下,告诉你就没什么意思了!

2. 位移操作的运算速度比乘/除法的运算速度高很多。

3. 在处理数据的乘/除法运算的时,采用位移运算可以获得较快的速度。

>>带符号右移 n>>n 将整型值带符号右移n位
<<带符号左移 n<<n 将整型值带符号左移n位

>>> 也是有的 自己找下吧!

简单的说明下,希望LZ能够明白!