I don't think so. If you care about what max and min are,
you should be using an explicitly sized type.
> Should they really be constants? They change between platforms.
So does syscall.OS.
> Is is possible to declare them as untyped constants? If so, how do you
> do it?
Sure. const MaxUint = 1<<32 - 1 or const MaxUint = 1<<64 - 1
would be the cleanest way.
There are "portable" but less readable ways too.
http://golang.org/src/pkg/big/arith.go
> Is the word length guaranteed to be the same for int and uint?
It looks like that is missing from the spec, but the answer is yes.
Russ
Not when it is on the right hand side of a shift.
In the example I pointed at only _m and _logS have types.
Russ
A possible counter-example is when you want to rotate an int, or
zigzag-encode an int as a uint, but you don't care whether it's a
32-bit or 64-bit int.
The zigzag encoding for 32-bit ints is: uint((i << 1) ^ (i >> 31)). It
maps [0, -1, 1, -2, 2, ...] to [0, 1, 2, 3, 4, ...]. Sure, you can
implement it with an if statement, but it might be faster as pure
bitwise operations.
That hardly ever comes up. I've only seen ZigZag encoding as applied
to protocol buffers, and you only ever deal with explicit int32/int64
types there (or their unsigned variants).
Dave.
I haven't needed it myself, but you might want to zigzag encode an int
(not an int32) if you want to use the resultant value as a slice index
(i.e. map from signed ints near zero to something). Sure, you could
convert to int64, do zigzag64, and convert back to int, but on a
32-bit processor it'd be faster to just do zigzag. Alternatively (for
the map of signed ints example), you could just add an index bias, but
then you have a lower bound on the possible indices, or you have to
manage changing the bias.
Similarly, I could imagine hashing a stream of ints as "hash =
rotateLeft(hash, 5) ^ i", and the rotateLeft implementation depends on
sizeof(int). It's not a great hash, but it's quick and simple.
I'm not saying that these use cases are common, I'm just suggesting
use cases for a IntBits constant.
Another use case for MinInt (as opposed to MinInt32) is if you want to write:
func max(a []int) int {
var x int = math.MinInt
for _, y := range a {
if x < y {
x = y
}
}
return x
}