Re: [go-nuts] Understanding least significant byte operation

2017-07-25 Thread Pablo Rozas Larraondo
Thanks Matt. That's a very good explanation about the internal bit representation of integers and answers perfectly my question. I really appreciate your help in understanding these operations. Cheers, Pablo On Wed, Jul 26, 2017 at 2:07 PM, Pablo Rozas Larraondo < p.rozas.larrao...@gmail.com>

Re: [go-nuts] Understanding least significant byte operation

2017-07-25 Thread Pablo Rozas Larraondo
Thanks Bakul, I also understand now what Matt Harden was referring to with his comment. At a bit level both operations cause the same effect on the underlying bits of the variable but it's meaning changes depending on the type. All clear! Cheers, Pablo On Wed, Jul 26, 2017 at 1:51 PM, Bakul Shah

Re: [go-nuts] Understanding least significant byte operation

2017-07-25 Thread Matt Harden
No, it's not interpreted differently. In 2's complement arithmetic, addition is the same operation on the underlying bits, whether the number is signed or unsigned. The same goes for negation and subtraction. There is a difference if you consider overflow, but Go doesn't have overflow of the

Re: [go-nuts] Understanding least significant byte operation

2017-07-25 Thread Bakul Shah
[Sorry, didn't see your original response] In the Go *language* (not just the compiler), when x is an unsigned int, -x is treated as a "shorthand" for ^x+1. Another interpretation is that for finite ints all arithmetic operations are modulo the word size. Thus -1 modulo 2^64 is 0x. You

Re: [go-nuts] Understanding least significant byte operation

2017-07-25 Thread Pablo Rozas Larraondo
Ok, I might be understanding this differently then: The -1 * x operation cannot be defined for unsigned integers as -1 overflows. Therefore, -x in the case of unsigned types is interpreted differently by the compiler, resulting in a ^x + 1 operation. Is that a correct assumption? By the way,

Re: [go-nuts] Understanding least significant byte operation

2017-07-25 Thread Matt Harden
Both statements are true for both signed and unsigned integers. On Mon, Jul 24, 2017, 04:11 Pablo Rozas Larraondo < p.rozas.larrao...@gmail.com> wrote: > Thanks Bakul, I think I have a better understanding of what's going on > after reading your response. > > Is it correct to say that the Go

Re: [go-nuts] Understanding least significant byte operation

2017-07-24 Thread Pablo Rozas Larraondo
Thanks Bakul, I think I have a better understanding of what's going on after reading your response. Is it correct to say that the Go compiler treats the prepended minus sign differently depending on the variable being a signed or an unsigned integer? By looking at this example:

Re: [go-nuts] Understanding least significant byte operation

2017-07-24 Thread Jan Mercl
On Mon, Jul 24, 2017 at 12:25 AM John Souvestre wrote: > That looks like the least significant set bit to me. To me too ;-) But I was responding to your > On Sun, Jul 23, 2017 at 7:26 PM John Souvestre wrote: > >> I believe that the result is the least

RE: [go-nuts] Understanding least significant byte operation

2017-07-23 Thread John Souvestre
John Souvestre - New Orleans LA From: Jan Mercl [mailto:0xj...@gmail.com] Sent: 2017 July 23, Sun 13:52 To: John Souvestre; golang-nuts@googlegroups.com Subject: Re: [go-nuts] Understanding least significant byte operation On Sun, Jul 23, 2017 at 7:26 PM John Souvestre &l

Re: [go-nuts] Understanding least significant byte operation

2017-07-23 Thread Bakul Shah
This is a standard trick to find the least significant set bit in a word. Only works for 2's complement numbers! -x == ~x+1 For example: x = 0011b (24), ~x+1 = 1100+1 = 1101. Adding them yields 0001; thus only the least significant set bit remains set. Note that func LSB(x

Re: [go-nuts] Understanding least significant byte operation

2017-07-23 Thread Jan Mercl
On Sun, Jul 23, 2017 at 7:26 PM John Souvestre wrote: > I believe that the result is the least significant bit, not byte. Does not look like that: https://play.golang.org/p/thzUaazLSp -- -j -- You received this message because you are subscribed to the Google Groups

RE: [go-nuts] Understanding least significant byte operation

2017-07-23 Thread John Souvestre
] Understanding least significant byte operation 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 opera

Re: [go-nuts] Understanding least significant byte operation

2017-07-23 Thread Ayan George
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

[go-nuts] Understanding least significant byte operation

2017-07-23 Thread Pablo Rozas Larraondo
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"