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
zzhang2006ProfessorAsked:
Who is Participating?
 
Gerwin Jansen, EE MVETopic Advisor Commented:
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
 
Gerwin Jansen, EE MVETopic Advisor Commented:
0
 
zzhang2006ProfessorAuthor 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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
zzhang2006ProfessorAuthor Commented:
Perfect!

Thank you, Gerwin!

Ze
0
 
zzhang2006ProfessorAuthor Commented:
Perfect, thank Gerwin!

ZE
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
You're welcome ;)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.