0

I am looking for a way to fill a binary array in vb.net by binary numbers. I filled the array line by line but I need to fill the array by using a loop

 DayArray(0) = "000"
 DayArray(1) = "001"
 DayArray(2) = "010"
 DayArray(3) = "011"
 DayArray(4) = "100"
 DayArray(5) = "101"
 DayArray(6) = "110"
 DayArray(7) = "111"

Any idea please??

Vasfed
  • 18,013
  • 10
  • 47
  • 53
S.Ozan
  • 51
  • 6
  • 1
    What you have there is an array of string.. Maybe you're looking for a [BitArray](https://msdn.microsoft.com/en-us/library/system.collections.bitarray(v=vs.110).aspx) ? Or take a look at [this](http://stackoverflow.com/questions/4048443/how-to-convert-decimal-number-to-binary-number-with-fixed-bits). – the_lotus Jun 30 '16 at 12:07
  • Can you please describe what you want to achieve? Storing "bit" values in a string seems like a code smell... – Alex B. Jun 30 '16 at 12:59
  • dear I need to input a number for the array size n then I need to generate an array with a bits starting from 0 till n-1. for example if the size that entered is 5 then the array should be [000,001,010,011,100]. – S.Ozan Jul 08 '16 at 19:06

2 Answers2

1

As an array of strings? Increment a counter and have a function to convert that int to binary string..I believe ToString can do it..or maybe the Convert class -- look for the one where you provide 'base' (ie. 2 for binary, 16 for hex, etc). And apparently you only want last 3 digits: use SubString() of string class.

ABuckau
  • 319
  • 4
  • 8
  • Thank you. But I think that I need it an array of bits – S.Ozan Jul 08 '16 at 19:10
  • Googling 'Vb.net array of bits' yields some good results: first one is about a class named BitArray and is by Microsoft (seems legit). You say you want an array of bits, but you try to store numbers other than 0 or 1, which a (binary) bit can not do. Use a larger data type and bit-manipulation operators (and, or) to get/set specific bits. I bet that class by Microsoft does this for any length of data. – ABuckau Jul 08 '16 at 23:21
  • I got your comment. I'll do a search for byte array as you mentioned. Hopefully it goes well with it. Thank you very much. – S.Ozan Jul 09 '16 at 16:39
  • I didn't say "byte" array..though that would be part of a good solution. Good luck. – ABuckau Jul 09 '16 at 16:48
  • I mean BitArray sorry. Thanks – S.Ozan Jul 11 '16 at 14:15
1
Dim DayArr(8) As String
For b As Integer = 0 To 8
    DayArr(b) = Convert.ToString(b, 2).PadLeft(3, "0"c)
Next

The Convert.ToString(b, 2) trims the leading zeros, so we need the PadLeft to make each string exactly three characters long.

Bert Cushman
  • 771
  • 2
  • 8
  • 27
  • Thank you very much , but I need the DayArr() as array of bits. So that I can use it for searching – S.Ozan Jul 08 '16 at 19:14
  • You asked a question, and I answered it. It sounds like you didn't ask the right question. An integer is simply an array of bits, and most languages provide mechanisms for working with integers at the bit level. You can search for "Logical/Bitwise Operators" to learn more about how to work with the individual bits in an integer, or you could use the BitArray mentioned above. – Bert Cushman Jul 10 '16 at 14:58
  • Thanks Bert. Yes you are absolutely right, it was my mistake I did not describe the question. – S.Ozan Jul 11 '16 at 14:19