Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go shift count too large

Tags:

go

In Go's constant specification, it is mentioned that:

Numeric constants represent exact values of arbitrary precision and do not overflow.

So I tried

const VeryVeryBigNumber = 1 << 200

and it works. However, the biggest shift count I could try is 511 and using 512 will throw:

shift count too large: 512.

What does 512 represents? I have no intention to use it, I just want to know why is it limited to 511 in my machine (I'm using ubuntu 64 bit and go 1.9.2)?

Thanks

like image 825
kasyauqi Avatar asked Oct 24 '25 16:10

kasyauqi


1 Answers

512 is kind of an arbitrary limit. The only thing the spec says is:

Implementation restriction: Although numeric constants have arbitrary precision in the language, a compiler may implement them using an internal representation with limited precision. That said, every implementation must:

Represent integer constants with at least 256 bits.

Unfortunately, the comments around the limits don't give a reason.

At some point, a limit has to be used. I would recommend sticking to the required 256.

like image 83
Marc Avatar answered Oct 26 '25 07:10

Marc