Solved

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

Posted on 2016-10-27
6
40 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
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.

705 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

21 Experts available now in Live!

Get 1:1 Help Now