[go-nuts] Re: constant 256 overflows byte
On Tuesday, 7 March 2017 19:02:59 UTC-5, Eric Brown wrote: > > memoryBlock := make([]byte, 4) > > binary.LittleEndian.PutUint32(memoryBlock, 12345678) > > > > memoryBlock[2] = 256 - (187 - memoryBlock[2]) ' results in error: > constant 256 overflows byte > memoryBlock[2] = 255 - (187 - memoryBlock[2]) + 1 ' results in no errors > > > Why is this, and what would be your suggested method of handling this? > I don't like having to do the later as it just looks sloppy. > > Byte operations work modulo 256, so adding 256 does nothing. Using that, you can simplify your expression to this: memoryBlock[2] -= 187 https://play.golang.org/p/5ljs4wIeq9 -- Paul Hankin -- 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.
Re: [go-nuts] Re: constant 256 overflows byte
memoryBlock[2] += (256-187) Am Mittwoch, 8. März 2017 01:48:08 UTC+1 schrieb Eric Brown: > > Actually, I didn't catch the int() in your example. I tried it, and it > worked! Thank you... > > On Tuesday, March 7, 2017 at 6:47:11 PM UTC-6, Eric Brown wrote: >> >> Thanks for the reply, Ayan. I thought so too; however, I previously >> tried this... and it resulted in the same error. >> >> On Tuesday, March 7, 2017 at 6:34:27 PM UTC-6, Ayan George wrote: >>> >>> >>> >>> On 03/07/2017 07:06 PM, Eric Brown wrote: >>> > By the way, this is code snipped from within a conditional check, so >>> > memoryBlock[2] will always be < 187 in this situation. >>> > >>> > On Tuesday, March 7, 2017 at 6:02:59 PM UTC-6, Eric Brown wrote: >>> > >>> > memoryBlock := make([]byte, 4) >>> > binary.LittleEndian.PutUint32(memoryBlock, 12345678) >>> > >>> > memoryBlock[2] = 256 - (187 - memoryBlock[2]) ' results in error: >>> > constant 256 overflows byte >>> > memoryBlock[2] = 255 - (187 - memoryBlock[2]) + 1 ' results in no >>> > errors >>> > >>> >>> Sorry, I only needed to cast the variable: >>> >>>memoryBlock[2] = byte(256 - (187 - int(memoryBlock[2]))) >>> >> -- 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.
Re: [go-nuts] Re: constant 256 overflows byte
Actually, I didn't catch the int() in your example. I tried it, and it worked! Thank you... On Tuesday, March 7, 2017 at 6:47:11 PM UTC-6, Eric Brown wrote: > > Thanks for the reply, Ayan. I thought so too; however, I previously tried > this... and it resulted in the same error. > > On Tuesday, March 7, 2017 at 6:34:27 PM UTC-6, Ayan George wrote: >> >> >> >> On 03/07/2017 07:06 PM, Eric Brown wrote: >> > By the way, this is code snipped from within a conditional check, so >> > memoryBlock[2] will always be < 187 in this situation. >> > >> > On Tuesday, March 7, 2017 at 6:02:59 PM UTC-6, Eric Brown wrote: >> > >> > memoryBlock := make([]byte, 4) >> > binary.LittleEndian.PutUint32(memoryBlock, 12345678) >> > >> > memoryBlock[2] = 256 - (187 - memoryBlock[2]) ' results in error: >> > constant 256 overflows byte >> > memoryBlock[2] = 255 - (187 - memoryBlock[2]) + 1 ' results in no >> > errors >> > >> >> Sorry, I only needed to cast the variable: >> >>memoryBlock[2] = byte(256 - (187 - int(memoryBlock[2]))) >> > -- 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.
Re: [go-nuts] Re: constant 256 overflows byte
Thanks for the reply, Ayan. I thought so too; however, I previously tried this... and it resulted in the same error. On Tuesday, March 7, 2017 at 6:34:27 PM UTC-6, Ayan George wrote: > > > > On 03/07/2017 07:06 PM, Eric Brown wrote: > > By the way, this is code snipped from within a conditional check, so > > memoryBlock[2] will always be < 187 in this situation. > > > > On Tuesday, March 7, 2017 at 6:02:59 PM UTC-6, Eric Brown wrote: > > > > memoryBlock := make([]byte, 4) > > binary.LittleEndian.PutUint32(memoryBlock, 12345678) > > > > memoryBlock[2] = 256 - (187 - memoryBlock[2]) ' results in error: > > constant 256 overflows byte > > memoryBlock[2] = 255 - (187 - memoryBlock[2]) + 1 ' results in no > > errors > > > > Sorry, I only needed to cast the variable: > >memoryBlock[2] = byte(256 - (187 - int(memoryBlock[2]))) > -- 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.
Re: [go-nuts] Re: constant 256 overflows byte
On 03/07/2017 07:06 PM, Eric Brown wrote: > By the way, this is code snipped from within a conditional check, so > memoryBlock[2] will always be < 187 in this situation. > > On Tuesday, March 7, 2017 at 6:02:59 PM UTC-6, Eric Brown wrote: > > memoryBlock := make([]byte, 4) > binary.LittleEndian.PutUint32(memoryBlock, 12345678) > > memoryBlock[2] = 256 - (187 - memoryBlock[2]) ' results in error: > constant 256 overflows byte > memoryBlock[2] = 255 - (187 - memoryBlock[2]) + 1 ' results in no > errors > Sorry, I only needed to cast the variable: memoryBlock[2] = byte(256 - (187 - int(memoryBlock[2]))) -- 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.
Re: [go-nuts] Re: constant 256 overflows byte
On 03/07/2017 07:06 PM, Eric Brown wrote: > memoryBlock[2] = 256 - (187 - memoryBlock[2]) ' results in error: > constant 256 overflows byte So since you're assigning to a byte I think Go is treating all of the constants as bytes. A byte can only encode between 0 and 255. You need an extra bit (ie, more than a byte) to store 256. Something like the following works but might be silly: memoryBlock[2] = byte(int(256) - (187 - int(memoryBlock[2]))) -- 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.
[go-nuts] Re: constant 256 overflows byte
By the way, this is code snipped from within a conditional check, so memoryBlock[2] will always be < 187 in this situation. On Tuesday, March 7, 2017 at 6:02:59 PM UTC-6, Eric Brown wrote: > > memoryBlock := make([]byte, 4) > > binary.LittleEndian.PutUint32(memoryBlock, 12345678) > > > > memoryBlock[2] = 256 - (187 - memoryBlock[2]) ' results in error: > constant 256 overflows byte > memoryBlock[2] = 255 - (187 - memoryBlock[2]) + 1 ' results in no errors > > > Why is this, and what would be your suggested method of handling this? > I don't like having to do the later as it just looks sloppy. > > -- 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.