On 07/23/2017 08:50 AM, Pablo Rozas Larraondo wrote:
> I have seen Go code using this function to find out the least 
> significant byte of unsigned integers:
> 
> func LSB(ci uint64) uint64 { return uint64(ci) & -uint64(ci) }
> 
> This function works fine but I wonder why, if call the same AND 
> operation, it results in an error: "constant -X overflows uint64"
> 
> Here is a playground example to illustrate this: 
> https://play.golang.org/p/_0EYtlLnmG
> 
> Does anyone know what changes when -uint64() is called in a return 
> statement? How a negative uint should be interpreted?
> 


(-uint64(24)) is a constant that can't be represented by a uint64.

I think the relevant bit from the spec is:

"The values of typed constants must always be accurately representable
as values of the constant type. The following constant expressions are
illegal..."

BUT, the rules for expressions that cause overflow are well defined and
operations on the variable 'ci' abide by them.

So I think it is a matter of compile-time correctness vs runtime behavior.

The following also works:

  https://play.golang.org/p/GYREFdz8-M

That is AFAIK. Someone please correct me if I'm wrong.

-ayan

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to