3

So I'm new to C# and I've searched but I just don't know what I'm searching for.

I have a boolean variable called inputs that has saved 32 true/false bits from a ModBus interface library.

Here's an image explaining my 32 true/false values. Basically those 32 true/false values stored in input. I need to collate them into a single 32-bit integer.

What I want to know is how to collate these 32 values into a single 32 bit string i.e (0000 0000 0000 1000 1000 1000 1000 1000) which will obviously be a 32 bit integer and give me a decimal number of 88888.

And I'm just stuck now because I'm not sure what command it is I should be using.

William
  • 1,993
  • 2
  • 23
  • 40
Bidaum92
  • 31
  • 6
  • And `Int32` or `UInt32`? – ryanyuyu Jan 30 '16 at 00:19
  • I've just added an edit with an image. Is this more clear? Basically those 32 true/falses stored in input. I need to collate them into a single 32 bit integer. Which will then give me a number at the end. – Bidaum92 Jan 30 '16 at 00:25
  • You want to know how many true & false boelans you have? a boelan variable can only be treu/false. If you want to know how many are true and how many are false then you have to create a loop and two int variables called: "int AmountTrue" "int Amountfalse" and add value to this variable depending on the loop target – ThunD3eR Jan 30 '16 at 00:26
  • Sorry no I don't want to know how many are true and false. I want to be able to collate every 1 and 0 into a single 32 bit string i.e (0000 0000 0000 1000 1000 1000 1000 1000) which will obviously be a 32 bit integer and give me a decimal number of 88888 – Bidaum92 Jan 30 '16 at 00:33
  • As usual, .NET has a type for that. BitVector32. – Hans Passant Jan 30 '16 at 02:11
  • This isn't a duplicate. The first "duplicate" asks for the other direction of conversion (and most of the answers there are messily string-based). The second "duplicate" is explicitly about starting from a string, not a bool sequence. There are at least two nice, non-string-based answers to be offered here. You could use `BitVector32` as Hans suggests, but it's a little more imperative (especially without [MoreLinq](https://morelinq.github.io/1.x/ref/api/html/M_MoreLinq_MoreEnumerable_ForEach__1_1.htm)) than simply using `bitsHighToLow.Aggregate(0, (i, b) => (i << 1) + (b ? 1 : 0))`. – William Oct 17 '16 at 13:12

4 Answers4

5

Just some bit manipulation:

uint i = 0;
var boolArr = new[] { true, false, true }; // 0b101
foreach (var bit in boolArr)
{
    // perform a bitwise left shift by 1 position
    // equivalent to multiplying i by 2
    i <<= 1;

    // store the bool value in the LSB of i
    i |= (uint)(bit ? 1 : 0);
}
Console.WriteLine(i);

Will output a value of 5.

Or a one-liner:

val = Convert.ToUInt32(string.Join("",
          bools.Select(b => b ? 1 : 0)), 2);
James Buck
  • 1,640
  • 9
  • 13
0

If you want a better understanding (James seems to be correct), have a look at this (you can just copy paste into Linqpad, otherwise, remove Dump() calls, and use ToString()):

// Initialize variables
bool[] ba = new bool[24];
Random r = new Random();
StringBuilder sb = new StringBuilder();
Int32 i32 =0;

// Initialize array
for (int i = 0; i < ba.Length; i++)
{
    var d = r.Next(0,10);
    ba[i] = (d < 5) ? true : false;
}

// Dump to string: 
for (int i = 0; i < ba.Length; i++)
{
    sb.Append( ba[i] ? 1 : 0);
}

// Build your int
for (int i = 0; i < ba.Length; i++)
{
    // Shift left
    i32 = i32 << 1;
    // Add your current value
    i32+= ba[i] ? 1 : 0;
}

// Outputs
ba.Dump("array output");
sb.ToString().Dump("as a string");
i32.Dump("as an int");

The following should also be helpful : << Operator (C# Reference) , or this question.

Community
  • 1
  • 1
Noctis
  • 11,507
  • 3
  • 43
  • 82
0

Once you have a string value which is in bits, you just need to convert that string using

  int output = Convert.ToInt32(input, 2);

It's been answered over here.

Convert binary string into integer

Community
  • 1
  • 1
Maertin
  • 384
  • 1
  • 8
0

I think this is what you want. booleans would be your bool array, then ints, is initialized (needs to) with the length of the bool array, for being the same. i is just the counter we use in the foreach loop. Then, you increment i and convert one bool value from the array to an integer, and save it on the int array. The last line joins all the integers. In the example, knowing that True = 1 and False = 0, you have TFTF, then, ints = 1, 0, 1, 0 and int result = 1010

                bool[] booleans = { true, false, true, false }; // Your bool array
                int[] ints = new int[booleans.Length];  // Integer equivalent of "booleans"
                int i = -1; // Counter for "foreach" loop
                foreach (bool boo in booleans)
                {
                    i++;
                    ints[i] = Convert.ToInt32(boo); // Convert boolean into binary
                }
                int result = Convert.ToInt32(string.Join("", ints)); // Concatenate them

I recommend you to take a short C# course, you'll learn a lot more, anyways, I hope this is what you wanted, if you want an integer array, just delete the last line. Please next time, include more information in the post.

TheCrimulo
  • 433
  • 1
  • 4
  • 14
  • Yes I do need a short course.. just so I can understand the terminology.. and then be able to actually use my google-fu better! Apologies for the lack of information.. once again it's because of a lack of knowledge on my part. – Bidaum92 Jan 30 '16 at 01:12