none
Bitarray count and length RRS feed

  • Question

  • Hello,

    Could you please explain about my code? why length and count property in Bitarray be 32 and no differences between 5 and 10 ? but bool is 1

    32 means 32 bit?

    I confused.

    using System;
    using System.Collections;
    using System.Collections.Concurrent;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace CuncurrentDictionary
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                int[] myInt = { 10 };
                int[] INT = { 5 };
                int[] iNt = { 10, 5 };
                bool[] b = { true };
    
                BitArray Bit1 = new BitArray(myInt);
                BitArray Bit2 = new BitArray(INT);
                BitArray Bit3 = new BitArray(iNt);
                BitArray Bit4 = new BitArray(b);
    
    
                Console.WriteLine("Length Bit1 is " + Bit1.Length);
                Console.WriteLine("Length Bit2 is " + Bit2.Length);
                Console.WriteLine("Length Bit3 is " + Bit3.Length);
                Console.WriteLine("Length Bit4 is " + Bit4.Length);
    
                Console.WriteLine();
    
                Console.WriteLine("Count Bit1 is " + Bit1.Count);
                Console.WriteLine("Count Bit2 is " + Bit2.Count);
                Console.WriteLine("Count Bit3 is " + Bit3.Count);
                Console.WriteLine("Count Bit4 is " + Bit4.Length);
    
                Console.ReadKey();
            }
        }
    }

    • Edited by Arash_89 Wednesday, March 21, 2018 11:15 AM
    Wednesday, March 21, 2018 11:08 AM

Answers

  • The first two variables myint and INT are both declaring an array of integers each containing just a single (32-bit) integer element (10 and 5, respectively).

    Hence both are 32 bits long.

    The third variable iNt contains 2 (32-bit) integer elements of 10 and 5. Hence 64 bits in total.

    Lastly, your boolean array contains a single boolean value of true. Booleans can be treated as simple on/off switches and only require a single bit. Hence length 1.

    Note - did you actually mean to declare integer arrays with 10 elements and 5 elements instead a 1-element array containing the value 10 and a 1-element array containing the value 5? If so then you actually meant:

    int[] myInt= new int[10];
    int[] INT= new int[5];
    int[,] iNt = new int[10,5]; // 2-dimensional array

    • Marked as answer by Arash_89 Wednesday, March 21, 2018 11:51 AM
    Wednesday, March 21, 2018 11:28 AM
  • Ah, I didn't actually check but the BitArray cannot take a multi-dimensional array. Check here for the allowable constructors.

    If, for some reason, you need to perform BitArray operations operations on a 2-dimensional array you would need to 'flatten' your 2-dimensional array into a 1-dimensional array first. E.g. A 10 by 5 array could be copied out into a 1-dimensional array of 50 elements (10 multiplied by 5).

    • Marked as answer by Arash_89 Wednesday, March 21, 2018 2:14 PM
    Wednesday, March 21, 2018 12:08 PM

All replies

  • The first two variables myint and INT are both declaring an array of integers each containing just a single (32-bit) integer element (10 and 5, respectively).

    Hence both are 32 bits long.

    The third variable iNt contains 2 (32-bit) integer elements of 10 and 5. Hence 64 bits in total.

    Lastly, your boolean array contains a single boolean value of true. Booleans can be treated as simple on/off switches and only require a single bit. Hence length 1.

    Note - did you actually mean to declare integer arrays with 10 elements and 5 elements instead a 1-element array containing the value 10 and a 1-element array containing the value 5? If so then you actually meant:

    int[] myInt= new int[10];
    int[] INT= new int[5];
    int[,] iNt = new int[10,5]; // 2-dimensional array

    • Marked as answer by Arash_89 Wednesday, March 21, 2018 11:51 AM
    Wednesday, March 21, 2018 11:28 AM
  • Thank you for your reply,

    When declare 2-dimensional array and i want pass to the Bitarray as constructor i have an error.

    Wednesday, March 21, 2018 12:02 PM
  • Ah, I didn't actually check but the BitArray cannot take a multi-dimensional array. Check here for the allowable constructors.

    If, for some reason, you need to perform BitArray operations operations on a 2-dimensional array you would need to 'flatten' your 2-dimensional array into a 1-dimensional array first. E.g. A 10 by 5 array could be copied out into a 1-dimensional array of 50 elements (10 multiplied by 5).

    • Marked as answer by Arash_89 Wednesday, March 21, 2018 2:14 PM
    Wednesday, March 21, 2018 12:08 PM