Bash decimal to binary conversion explanation - Unix & Linux Stack Exchange most recent 30 from unix.stackexchange.com 2019-08-24T00:21:26Z https://unix.stackexchange.com/feeds/question/203003 http://www.creativecommons.org/licenses/by-sa/3.0/rdf https://unix.stackexchange.com/q/203003 2 Bash decimal to binary conversion explanation iyiyy https://unix.stackexchange.com/users/115110 2015-05-12T17:47:19Z 2015-05-12T23:38:42Z <p>When converting decimal values to their binary representation, it is possible to use the following commands:</p> <pre><code>val=12 D2B=({0..1}{0..1}{0..1}{0..1}{0..1}) echo \${D2B[\$val]} </code></pre> <p>While this works, I am unable to understand or find why it does so. Would be great if someone could explain it or point me to a resource that does.</p> https://unix.stackexchange.com/questions/203003/-/203004#203004 3 Answer by cuonglm for Bash decimal to binary conversion explanation cuonglm https://unix.stackexchange.com/users/38906 2015-05-12T17:56:12Z 2015-05-12T18:02:30Z <p><a href="http://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html" rel="nofollow">Brace expansion</a> in form <code>{x..y}</code> give you all possible characters from range x to y. In this case, <code>{0..1}</code> give you <code>0</code> and <code>1</code>. Combine two pieces give you <code>2^2</code> four possible values:</p> <pre><code>\$ printf %s\\n {0..1}{0..1} 00 01 10 11 </code></pre> <p>Combine five pieces give you <code>2^5</code> thirty two possible values from 0 to 32 in binary form:</p> <pre><code>\$ printf %s\\n {0..1}{0..1}{0..1}{0..1}{0..1} 00000 00001 00010 00011 00100 00101 00110 00111 01000 01001 01010 01011 01100 ..... 11110 11111 </code></pre> <p>Saving all values in <code>D2B</code> array, with decimal index corresponding to it binary value. Accessing index 12 <code>\${D2B[\$val]}</code> gave you the binary value of 12 decimal.</p> <p>Actually, you can make it shorter with <code>{x,y}</code>:</p> <pre><code>D2B=({0,1}{0,1}{0,1}{0,1}{0,1}) </code></pre> https://unix.stackexchange.com/questions/203003/-/203005#203005 2 Answer by Chris Down for Bash decimal to binary conversion explanation Chris Down https://unix.stackexchange.com/users/10762 2015-05-12T17:59:48Z 2015-05-12T17:59:48Z <p>First, note that this is quite a memory inefficient way to do this. You might, instead, consider the answers on <a href="https://unix.stackexchange.com/q/65280/10762">this</a> question.</p> <hr> <p>As for your question, though...</p> <pre><code>D2B=({0..1}{0..1}{0..1}{0..1}{0..1}) </code></pre> <p>This line creates an array called <code>D2B</code> that contains all possible binary values from 00000 to 11111. Brace expansions work like this:</p> <pre><code>% printf '%s %s %s\n' {0..2}{0..2}{0..2} 000 001 002 010 011 012 020 021 022 100 101 102 110 111 112 120 121 122 200 201 202 210 211 212 220 221 222 </code></pre> <p>Due to the fact that brace expansions are expanded from the first to the last when they are adjoined, the array looks something like this:</p> <pre><code>00000 00001 00010 .... 11101 11110 11111 </code></pre> <p>The indexes of these values correspond to the binary values contained within them, since here, brace expansions and the mathematic representations of numbers use the same rules.</p> <p>As such, index 0 is <code>00000</code>, index 5 is <code>00101</code>, and so on. Since you pass <code>\$val</code> as the index to use, the binary corresponding to your value is echoed.</p>