有一byte数值(例如:129),求这值的第N(N>=1,N<=8)位是否为1(如129的第8位为1)的bool表达式。

解决方案 »

  1.   

    return Convert.ToByte(129).ToString().SubString(7,1) == "1";
      

  2.   

    int temp=129;
    int n=7;
    for(int i=1;i<=8;i++)
    {
    byte bt=(byte)temp;
    bt=(byte)(bt>>(8-i));
    Console.WriteLine((bt&0x01)>0?true:false);
    }
    思路基本上是这个样子,具体性能如何,不知道
      

  3.   

    private bool f(byte bt, int n)
    {
    string str = Convert.ToByte(bt).ToString();
    byte ch = (byte)(0x01<<n-1);
    return (bt | ch) == ch;
    }
      

  4.   

    private void button1_Click(object sender, System.EventArgs e)
    {
    byte Value = 129;
    int n = 7; // n的取值为0至7,而非1至8
    Console.WriteLine(Convert.ToBoolean(Value & (byte)Math.Pow(2,n)).ToString());
    }
      

  5.   

    '仅限非负整数
    '十进制数转换成二进制数
    Public Function DecToBin(ByVal value As Integer) As String
        If value = 0 Then Return "0"    Dim tempValue As Integer = value
        Dim tempString As String = ""    Dim modValue As Integer
        Do
            tempString += CStr(tempValue Mod 2)
            tempValue = tempValue \ 2
        Loop While (tempValue <> 0)    Dim i As Integer
        Dim returnChars As Char() = tempString.ToCharArray()
        Dim returnString As String = ""
        For i = tempString.Length - 1 To 0 Step -1
            returnString += returnChars(i)
        Next    Return returnString
    End Function
    以上是将十进制转换为二进制的字符,再在字符中判断N位是否为1就简单了
      

  6.   

    private bool f(byte bt, int n)
    {
    byte ch = (byte)(1<<n-1);
    return bt & ch > 0;
    }
      

  7.   

    private bool f(byte bt, int n)
    {
    byte ch = (byte)(1<<n-1);
    return bt & ch > 0;
    }
      

  8.   

    private bool XX(byte c,int N)
    {
    int iCompareValue=Math.Pow(2,N);
    if((int)c>=iCompareValue)
    {
    return true;
    }
    else
    {
    return false;
    }
    }
      

  9.   

    int num = 129;
    int N 8 ;
    int j=0
    for(int i =0;i<N;i++)
    {
    j=num%2;
    num = num /2;
    }
    return j==1;
    ----------
    佩服楼主ID和呢称的默契
      

  10.   

    汗啊,这个东西竟然这么多人不会写,那些写对的效率不高(比方楼上)那些效率高的却写错了(比方 Ivony )
      

  11.   

    public static bool IsSet(byte b, int n)
    {
    return (((b >> (n - 1)) & 1) != 0);
    }
      

  12.   

    用tiaoci(我挑刺,我快乐)提供的函数写的int和byte转换成位字符串
    #region GetIntToBitstring
    /// <summary>
    /// 把int值转化为位字符串
    /// </summary>
    /// <param name="intValue"></param>
    /// <returns></returns>
    public static string GetIntToBitstring(int intValue)
    {
    byte[] bs=BitConverter.GetBytes(intValue);
    System.Text.StringBuilder sb=new System.Text.StringBuilder();
    foreach(byte b in bs)
    {
    sb.Append(GetByteToBitstring(b));
    }
    return sb.ToString();
    }
    #endregion#region GetByteToBitstring
    /// <summary>
    /// 把byte值转化为位字符串
    /// </summary>
    /// <param name="byteValue"></param>
    /// <returns></returns>
    public static string GetByteToBitstring(byte byteValue)
    {
    System.Text.StringBuilder sb=new System.Text.StringBuilder();
    for(int i=8;i>0;i--)
    {
    bool isSet=IsSet(byteValue,i);
    sb.Append(isSet?"1":"0");
    }
    return sb.ToString();
    }
    #endregion#region IsSet
    /// <summary>
    /// 判断byte第N位上是否为1
    /// </summary>
    /// <param name="byteValue"></param>
    /// <param name="n"></param>
    /// <returns></returns>
    public static bool IsSet(byte byteValue, int n)
    {
    return (((byteValue >> (n - 1)) & 1) != 0);
    }
    #endregion
      

  13.   

    汗啊,这个东西竟然这么多人不会写,那些写对的效率不高(比方楼上)那些效率高的却写错了(比方 Ivony )==========================================================呵呵,我是顺手拿了楼上的一个改的,木测试。