# Just want to learn, how Linux D2B works, under Linux bash shell

On bash Shell was was able to convert a decimal number say, 123 to binary like this:

D2B=({0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1})
pi@Raspberry_~/Assembly_Codes \$ echo \${D2B[123]}
01111011

Well, that;s so nice, can anyone explain to me what is D2B?  Is it an built-in array or what?

Thanks

Ze
LVL 1
###### Who is Participating?

OK, I see, let me show you an example:

echo {0..1}{0..1} - this is expanding to all possible combinations:

00 01 10 11

00=zero, 01=one, 10=two, 11=three -> numbers in binary representation

if you put that set into an array:

DD=({0..1}{0..1})

and then look up a number you want to find in the array, you just index that array.

So if you want to find the binary representation of the number 2, you index the array like this:

echo \${DD[2]}

and you get this result:

10

Your example has 8 bits and you can use it to lookup binary values between 0 and 255. So your 123 is stored in the array as 01111011

Try showing the complete array like this:

echo \${D2B[*]}
0

0

ProfessorAuthor Commented:
Gerwin, thanks for point out the direction. Still don't get what is  0..1  and the indexing D2B[127] like an array.

Ze
0

ProfessorAuthor Commented:
Perfect!

Thank you, Gerwin!

Ze
0

ProfessorAuthor Commented:
Perfect, thank Gerwin!

ZE
0