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
zzhang2006Asked:
Who is Participating?
 
Gerwin Jansen, EE MVEConnect With a Mentor Topic 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 MVEConnect With a Mentor Topic Advisor Commented:
0
 
zzhang2006Author 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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
zzhang2006Author Commented:
Perfect!

Thank you, Gerwin!

Ze
0
 
zzhang2006Author Commented:
Perfect, thank Gerwin!

ZE
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
You're welcome ;)
0
All Courses

From novice to tech pro — start learning today.