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))256I'm producing an integer by shifting a bit. How far can I go?$ echo $((1<<80000))1Not this far, apparently. (1 is unexpected, and I'll return to it.) But,$ echo $((1<<1022))4611686018427387904is still positive. Not this, however:$ echo $((1<<1023))-9223372036854775808And one step further afield,$ echo $((1<<1024))1Why 1? And why the following?$ echo $((1<<1025))2$ echo $((1<<1026))4Would someone like to analyse this series?UPDATEMy machine:$ uname -aLinux 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
Bitwise shift and the largest integer in Bash
bash;arithmetic
Bash uses intmax_t variables for arithmetic. On your system these are 64 bits in length, so:$ echo $((1<<62))4611686018427387904which is100000000000000000000000000000000000000000000000000000000000000in binary (1 followed by 62 0s). Shift that again:$ echo $((1<<63))-9223372036854775808which is1000000000000000000000000000000000000000000000000000000000000000in binary (63 0s), in two's complement arithmetic.To get the biggest representable integer, you need to subtract 1:$ echo $(((1<<63)-1))9223372036854775807which is111111111111111111111111111111111111111111111111111111111111111in binary.As pointed out in ilkkachu's answer, shifting takes the offset modulo 64 on 64-bit x86 CPUs (whether using RCL or SHL), which explains the behaviour you're seeing:$ echo $((1<<64))1is equivalent to $((1<<0)). Thus $((1<<1025)) is $((1<<1)), $((1<<1026)) is $((1<<2))...(I'd originally written that the shift wrapped around, which is utter nonsense  it would be a rotation in that case, not a shift.)You'll find the type definitions and maximum values in stdint.h; on your system:/* Largest integral types.  */#if __WORDSIZE == 64typedef long int                intmax_t;typedef unsigned long int       uintmax_t;#else__extension__typedef long long int           intmax_t;__extension__typedef unsigned long long int  uintmax_t;#endif/* Minimum for largest signed integral type.  */# define INTMAX_MIN             (-__INT64_C(9223372036854775807)-1)/* Maximum for largest signed integral type.  */# define INTMAX_MAX             (__INT64_C(9223372036854775807))