Solved

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

Posted on 2016-10-27
6
59 Views
Last Modified: 2016-11-02
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
Comment
Question by:zzhang2006
  • 3
  • 3
6 Comments
 
LVL 37

Assisted Solution

by:Gerwin Jansen
Gerwin Jansen earned 500 total points
ID: 41863762
0
 
LVL 1

Author Comment

by:zzhang2006
ID: 41869150
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
 
LVL 37

Accepted Solution

by:
Gerwin Jansen earned 500 total points
ID: 41869179
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.

 
LVL 1

Author Comment

by:zzhang2006
ID: 41870964
Perfect!

Thank you, Gerwin!

Ze
0
 
LVL 1

Author Comment

by:zzhang2006
ID: 41870968
Perfect, thank Gerwin!

ZE
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 41870977
You're welcome ;)
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Daily system administration tasks often require administrators to connect remote systems. But allowing these remote systems to accept passwords makes these systems vulnerable to the risk of brute-force password guessing attacks. Furthermore there ar…
Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now