Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 100
  • Last Modified:

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
0
zzhang2006
Asked:
zzhang2006
  • 3
  • 3
2 Solutions
 
Gerwin Jansen, EE MVETopic 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
 
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now