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>
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
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
[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
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,
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
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:
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
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
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
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
] 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
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
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"
14 matches
Mail list logo