Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将short转换为bool[]?_C# - Fatal编程技术网

C# 如何将short转换为bool[]?

C# 如何将short转换为bool[]?,c#,C#,如何将C#中的short转换为boolean数组(bool[])?我正在寻找一个算法,多种选择将与他们的优点和缺点赞赏 例如,如果我有一个=132的短码,在二进制中是10000100 所以我想要一个数组{false,false,true,false,false,false,true} bool[] ToArrayOfBool(short source) { bool[] result = new bool[16]; ///Do Some stuff here ret

如何将C#中的short转换为boolean数组(bool[])?我正在寻找一个算法,多种选择将与他们的优点和缺点赞赏

例如,如果我有一个=132的短码,在二进制中是10000100

所以我想要一个数组{false,false,true,false,false,false,true}

bool[] ToArrayOfBool(short source)
{
    bool[] result = new bool[16];

    ///Do Some stuff here


    return result;
}
您可以使用:

这将生成一个
IEnumerable
,但您也可以将其转换为数组:

byte input = 132;
var bits = new BitArray(new byte[] { input }).Cast<bool>().ToArray();
foreach (var b in bits)
{
    Console.WriteLine(b);
}
您也可以将其保留为
short
,并使用如下扩展方法获取所需的位:

public static bool GetBit(this short This, int bitNumber)
{
    return (This & (1 << bitNumber)) != 0;
}

for (int i=0; i<8; i++)
{
    Console.WriteLine(input.GetBit(i));
}
public static bool GetBit(this short this,int bitNumber)
{
返回(这个&(11)线索


看起来您有三个步骤:

  • 首先使用
    myshort
    是您的值将其转换为二进制

  • 循环遍历字符串的每个字符,测试它是否等于1,并将每个字符添加到
    bool
    数组中

  • 反转数组(或者可以在步骤1后反转字符串)

  • 因为我很无聊,这里有一个例子:)

    for(int i=0;i<16;i++){
    //索引i处的单位位位掩码:0b0001、0b0010、0b0100等。。
    int bitmask=1
    使用系统;
    名称空间控制台EAPP1
    {
    班级计划
    {
    静态void Main(字符串[]参数)
    {
    控制台。WriteLine(“你好,世界!”);
    Console.WriteLine($“132={string.Join(“,”,ConvertToArray(132))});
    Console.ReadKey();
    }
    静态布尔[]变换阵列(短@短)
    {
    var结果=新布尔值[16];
    对于(int i=0;i<16;i++)
    {
    结果[i]=(@short&(short)1)==(short)1?真:假;
    @short=(short)(@short>>1);
    }
    返回结果;
    }
    }
    }
    
    多亏了@pm100,我才想到了这一点

    public bool[] ToArrayOfBool(short source)
    {
        bool[] result = new bool[16];
    
        for (int i = 0; i < 16; i++)
        {
            result[i] = (source & (i * i)) == i * i; 
        }
    
        return result;
    }
    
    public bool[]ToArrayOfBool(短源代码)
    {
    bool[]结果=新bool[16];
    对于(int i=0;i<16;i++)
    {
    结果[i]=(源和(i*i))==i*i;
    }
    返回结果;
    }
    
    您想将一个短数字转换为二进制表示形式吗?不一定是一个完全相同的数字,但肯定是相关的。添加了输入输出示例。很抱歉,为什么会有所有的反对票?
    public static bool GetBit(this short This, int bitNumber)
    {
        return (This & (1 << bitNumber)) != 0;
    }
    
    for (int i=0; i<8; i++)
    {
        Console.WriteLine(input.GetBit(i));
    }
    
    var bits = new bool[16];
    if(i &x0001) bits[0] = true;
    if(i &x0002) bits[1] = true;
    
        short myshort = 132;
        Console.WriteLine("Short: " + myshort);
    
        var binarystring = Convert.ToString(myshort, 2);
        Console.WriteLine("Binary: " + binarystring);
    
        var boolarray = new bool[binarystring.Length];
    
        for (var i = 0; i < binarystring.Length; i++) {
            boolarray[i] = binarystring[i] == '1';          
        }
    
        Array.Reverse(boolarray);
    
        //output result     
        foreach (var b in boolarray) {
            Console.Write(b.ToString() + ", ");
        }
    
    Short: 132
    Binary: 10000100
    False, False, True, False, False, False, False, True,
    
    for (int i = 0; i < 16; i++) {
        // Single bit bitmask at index i: 0b0001, 0b0010, 0b0100 etc..
        int bitmask = 1 << i;
        // Check to see if the bit at that mask is set.
        result[i] = (source & bitmask) != 0;
    }
    
    using System;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Hello World!");
                Console.WriteLine($"132 = {string.Join(",", ConvertToArray(132))}");
                Console.ReadKey();
            }
    
            static bool[] ConvertToArray(short @short)
            {
                var result = new bool[16];
                for (int i = 0; i < 16; i++)
                {
                    result[i] = (@short & (short)1) == (short)1 ? true : false;
                    @short = (short)(@short >> 1);
                }
                return result;
            }
    
        }
    }
    
    public bool[] ToArrayOfBool(short source)
    {
        bool[] result = new bool[16];
    
        for (int i = 0; i < 16; i++)
        {
            result[i] = (source & (i * i)) == i * i; 
        }
    
        return result;
    }