3

Is there a way to find a value's 2 exponential form in bash.

For example if I input 512 it should result output as 9 meaning 2 ^ 9 is 512.

Any help here is immensely appreciated - Thanks

nss
  • 65
  • 4

3 Answers3

3

When I read the question, 512 is the input, and 9 is the output. Is is possible what is being asked here is the answer to "log_base_2(512)" which has an answer of "9". If so, then maybe this would help.

$ echo "l(512) / l(2)" | bc -l
9.00000000000000000008

The explanation of the math can be found here:

How do I calculate the log of a number using bc?

2

Using awk.

$ echo 512 | awk '{print log($1)/log(2)}'
9

Put that into a script (expo.sh):

#!/bin/bash

_num="$1"
expon=$(awk -v a="$_num" 'BEGIN{print log(a)/log(2)}')
if [[ $expon =~ ^[0-9]+\.[0-9]*$ ]]; then # Match floating points
    echo "$_num is not an exponent of 2"; # Not exponent if floating point
else 
    echo "$_num = 2^${expon}"; # print number
fi

Run:

$ ./expo.sh 512
512 = 2^9
$ ./expo.sh 21
21 is not an exponent of 2
iamauser
  • 11,119
  • 5
  • 34
  • 52
0

A fast way to check a number x is an 2 exponent is to check bitwise and x and x-1 and to exclude 0, x>0

((x>0 && ( x & x-1 ) == 0 )) && echo $x is a 2-exponent

using this algorithm: fast-computing-of-log2-for-64-bit-integers to compute log2

tab32=( 0  9  1 10 13 21  2 29
       11 14 16 18 22 25  3 30
        8 12 20 28 15 17 24  7
       19 27 23  6 26  5  4 31 )


log2_32() {
    local value=$1
    (( value |= value >> 1 ))
    (( value |= value >> 2 ))
    (( value |= value >> 4 ))
    (( value |= value >> 8 ))
    (( value |= value >> 16 ))
    log2_32=${tab32[(value * 16#7C4ACDD & 16#ffffffff)>>27]}
}

log2_32 262144 
echo "$log2_32"
Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36