This is an exploration question, meaning I'm not completely sure what this question is about, but I think it's about the biggest integer in Bash. Anyhow, I'll define it ostensively.
$ echo $((1<<8))
256
I'm producing an integer by shifting a bit. How far can I go?
$ echo $((1<<80000))
1
Not this far, apparently. (1 is unexpected, and I'll return to it.) But,
$ echo $((1<<1022))
4611686018427387904
is still positive. Not this, however:
$ echo $((1<<1023))
-9223372036854775808
And one step further afield,
$ echo $((1<<1024))
1
Why 1? And why the following?
$ echo $((1<<1025))
2
$ echo $((1<<1026))
4
Would someone like to analyse this series?
UPDATE
My machine:
$ uname -a
Linux tomas-Latitude-E4200 4.4.0-47-generic #68-Ubuntu SMP Wed Oct 26 19:39:52 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
Best Answer
Bash uses
intmax_t
variables for arithmetic. On your system these are 64 bits in length, so:which is
in binary (1 followed by 62 0s). Shift that again:
which is
in binary (63 0s), in two's complement arithmetic.
To get the biggest representable integer, you need to subtract 1:
which is
in binary.
As pointed out in ilkkachu's answer, shifting takes the offset modulo 64 on 64-bit x86 CPUs (whether using
RCL
orSHL
), which explains the behaviour you're seeing:is equivalent to
$((1<<0))
. Thus$((1<<1025))
is$((1<<1))
,$((1<<1026))
is$((1<<2))
...You'll find the type definitions and maximum values in
stdint.h
; on your system: