pythongobyte-shifting

How to bypass Go int64 value limits while shifting integers?


I'm trying with Go to get values of KiB, MiB, ..., ZiB, Yib which are respectively KibiByte, MebiByte, ..., ZebiByte, YobiByte.

My code in Golang is:

package main 
import ( 
    "fmt"
)

func main() {
    s := []string{"KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}

    for k,v := range(s) {
        fmt.Printf("%s: %v\n", v, 1 << uint64(10 * (k+1)))
    }
}

But, the values of ZiB and YiB overflows Go uint64 and this why I'm having this output:

KiB: 1024
MiB: 1048576
GiB: 1073741824
TiB: 1099511627776         // exceeds 1 << 32
PiB: 1125899906842624
EiB: 1152921504606846976
ZiB: 0                    // exceeds 1 << 64
YiB: 0                    // exceeds 1 << 64

Otherwise, with the same shifting logic in Python3 within this code:

a = ["KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
for k,v in enumerate(a):
    print("{}: {}".format(v, 1 << (10 *(k+1))))

The output is correct, like the output below:

KiB: 1024
MiB: 1048576
GiB: 1073741824
TiB: 1099511627776
PiB: 1125899906842624
EiB: 1152921504606846976
ZiB: 1180591620717411303424
YiB: 1208925819614629174706176

So, how can I bypass Go uint64 limits and get the correct values using shifting integers like what I can get from shifting integers using Python.

Thanks.


Solution

  • You can't work with numbers that require more than 64bits with a primitive uint64. Python has arbitrary precision integers, and to get the same in Go you need to use the math/big package.

    s := []string{"KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}
    
    one := big.NewInt(1)
    for k, v := range s {
        fmt.Printf("%s: %v\n", v, new(big.Int).Lsh(one, uint(10*(k+1))))
    }
    

    https://play.golang.org/p/i5v5P5QgQb