C#:
short 类型变量  i
我想取i的某一位index是不是1..如何编写算法?
...
谁会啊..在线等您指教

解决方案 »

  1.   

    private static bool CheckBit(int i,int bit)
    {
    //MessageBox.Show( CheckBit(4,3).ToString());
    int x=(int)System.Math.Pow(2,bit-1);
    return((i&x)!=0); }
      

  2.   

    private static bool CheckBit(int i,int bit)
    {
    return (((i>>(bit-1))&1)!=0); }
      

  3.   

    class Class1
    {
    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    //
    // TODO: 在此处添加代码以启动应用程序
    //
    short i = 2;
    ushort index = 3;
    bool res = check(i,index);
    Console.WriteLine("["+index.ToString()+"] = "+res.ToString());
    } public static bool check(short i,ushort index)
    {
    return (((i >> (index-1)) & (short)1) == 1);
    }
    }
    expl:
    short i = 2;——0000000000000010
    上述check()中,将上面的1这位视作第二位