Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash exponentiation limit

I want to understand the reason maximum value for $[a**b] operation in Bash.

$ echo $[2**62]
4611686018427387904
$ echo $[2**63]
-9223372036854775808
$ echo $[2**64]
0
like image 366
Сергей Хвощевский Avatar asked Jun 09 '26 05:06

Сергей Хвощевский


1 Answers

According to Shell Arithmetic in the Bash manual:

Evaluation is done in fixed-width integers with no check for overflow

So, on a 64-bit platform, for a signed integer, we wrap around at 263:

$ echo $((2**63-1))
9223372036854775807
$ echo $((2**63))
-9223372036854775808
like image 148
Benjamin W. Avatar answered Jun 11 '26 22:06

Benjamin W.