0

I'm trying to write a Bash function that is the inversion of the function in the answer https://stackoverflow.com/a/72687565/1277576.

The purpose is to obtain the decimal representation of a number from its binary representation in two's complement.

dec() {
  n=$(getconf LONG_BIT)
  x=$(echo "ibase=2; $1" | bc)
  echo "if ($x<2^($n-1)) $x else -$((~$x))-1" | bc
}

My issue is that it works only for negative binary integers (that is, when the most significant bit is equal to 1), while if fails for positive ones (that is, when the most significant bit is equal to 0):

$ dec 1111111111111111111111111111111111111111111111111111111111111111
-1
$ dec 1000000000000000000000000000000000000000000000000000000000000000
-9223372036854775808
$ dec 0000000000000000000000000000000000000000000000000000000000000001
(standard_in) 1: syntax error

It seems that the line echo "if ($x<2^($n-1)) $x else -$((~$x))-1" | bc contains a syntax error, but I don't understand what it is.

2
  • 1
    enable debug mode (set -xv) and review the output; you should find the echo is not generating what you think it is Commented Jun 20, 2022 at 18:10
  • 1
    In the failing example, echo generates echo 'if (1<2^(64-1)) 1 else --2-1'. I guess that the issue could be the double minus sign. Commented Jun 20, 2022 at 18:16

2 Answers 2

1
$ dec() {
  printf 'n=%d; ibase=2; v=%s; v-2^n*(v/2^(n-1))\n' "$(getconf LONG_BIT)" "$1"| bc
}
$ dec 1111111111111111111111111111111111111111111111111111111111111111
-1
$ dec 0000000000000000000000000000000000000000000000000000000000000001
1
Sign up to request clarification or add additional context in comments.

Comments

1

The solution:

dec() {
  n=$(getconf LONG_BIT)
  x=$(echo "ibase=2; $1" | bc)
  echo "if ($x<2^($n-1)) $x else -($((~$x))+1)" | bc
}

The problem was the double minus sign, as pointed in the comment Conditional IF statement syntax in BC.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.