FAQ
Is the only way to sign extend a value through use of casting between uint
and int?

e.g. if I want to sign-extend from the 16 bit boundary do I need to do the
following?

val := uint32(0x0000ffff)
val = uint32(int32(val << 16) >> 16)

Thanks!


--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Chris dollin at Dec 9, 2013 at 1:42 pm

    On 9 December 2013 13:28, matt wrote:

    Is the only way to sign extend a value through use of casting between uint
    and int?
    No.

    e.g. if I want to sign-extend from the 16 bit boundary do I need to do the
    following?

    val := uint32(0x0000ffff)
    val = uint32(int32(val << 16) >> 16)
      I'm not sure exactly what you're asking. If you have a signed 16-bit
    value and you want to extend it to a signed 32-bit value, a simple
    conversion [1] `int32(the16bitValue)` will do. If the 16-bit value is
    unsigned but you want to pretend that its signed and then sign-
    extend it, `int32(int16(theUnsigned16BitValue))` will do.

    Does that make sense?

    Chris

    [1] Not a "cast". Go doesn't have casts. What C rolls together under
         the cast umbrella, Go has conversions and type-assertions for.
         (And unsafe, but there be shark-encrusted waters.)

    --
    Chris "allusive" Dollin

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Matt at Dec 9, 2013 at 2:13 pm
    Agree your code works on type-size conversions, but, imagine a packed
    binary structure that contains an N-bit signed value; that you want to use
    as an M-bit signed value. Let's assume M is of a defined type-size
    (8/16/32/64).

    If N doesn't lie on a type-size boundary I think you need to explicitly
    shift it up until the top bit is in the most significant bit of the next
    largest type (e.g. for N == 21, you need to shift left by 11) and then
    type-convert to a signed type in order to keep the signedness when shifting
    back down to position 21. Hence the shift up and down in my previous
    example.

    // Matt

    On Monday, 9 December 2013 13:42:14 UTC, chris dollin wrote:

    On 9 December 2013 13:28, matt <matthew....@gmail.com <javascript:>>wrote:
    Is the only way to sign extend a value through use of casting between
    uint and int?
    No.

    e.g. if I want to sign-extend from the 16 bit boundary do I need to do
    the following?

    val := uint32(0x0000ffff)
    val = uint32(int32(val << 16) >> 16)
    I'm not sure exactly what you're asking. If you have a signed 16-bit
    value and you want to extend it to a signed 32-bit value, a simple
    conversion [1] `int32(the16bitValue)` will do. If the 16-bit value is
    unsigned but you want to pretend that its signed and then sign-
    extend it, `int32(int16(theUnsigned16BitValue))` will do.

    Does that make sense?

    Chris

    [1] Not a "cast". Go doesn't have casts. What C rolls together under
    the cast umbrella, Go has conversions and type-assertions for.
    (And unsafe, but there be shark-encrusted waters.)

    --
    Chris "allusive" Dollin
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Chris dollin at Dec 9, 2013 at 2:33 pm

    On 9 December 2013 14:13, matt wrote:

    Agree your code works on type-size conversions, but, imagine a packed
    binary structure that contains an N-bit signed value; that you want to use
    as an M-bit signed value.
    Ah, I see what you want now.

    I think the dancing shifts are the (Go, safe) way.

    Chris

    --
    Chris "allusive" Dollin

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Matt at Dec 9, 2013 at 2:48 pm
    Yes, I just wanted to check that I hadn't missed a go idiomatic way for
    doing this ;)

    Thanks!
    On Monday, 9 December 2013 14:33:09 UTC, chris dollin wrote:

    On 9 December 2013 14:13, matt <matthew....@gmail.com <javascript:>>wrote:
    Agree your code works on type-size conversions, but, imagine a packed
    binary structure that contains an N-bit signed value; that you want to use
    as an M-bit signed value.
    Ah, I see what you want now.

    I think the dancing shifts are the (Go, safe) way.

    Chris

    --
    Chris "allusive" Dollin
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Jan Mercl at Dec 9, 2013 at 1:42 pm

    On Mon, Dec 9, 2013 at 2:28 PM, matt wrote:
    Is the only way to sign extend a value through use of casting between uint
    and int?

    e.g. if I want to sign-extend from the 16 bit boundary do I need to do the
    following?

    val := uint32(0x0000ffff)
    val = uint32(int32(val << 16) >> 16)
    Do you perhaps mean something like http://play.golang.org/p/PshmCsuvQY
    or http://play.golang.org/p/1RY3dtM3HM ?

    -j

    PS: Go has no casts.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Matt at Dec 9, 2013 at 2:13 pm
    No, that looks entirely unrelated, but thanks :)


    On Monday, 9 December 2013 13:42:33 UTC, Jan Mercl wrote:

    On Mon, Dec 9, 2013 at 2:28 PM, matt <matthew....@gmail.com <javascript:>>
    wrote:
    Is the only way to sign extend a value through use of casting between uint
    and int?

    e.g. if I want to sign-extend from the 16 bit boundary do I need to do the
    following?

    val := uint32(0x0000ffff)
    val = uint32(int32(val << 16) >> 16)
    Do you perhaps mean something like http://play.golang.org/p/PshmCsuvQY
    or http://play.golang.org/p/1RY3dtM3HM ?

    -j

    PS: Go has no casts.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Jsor at Dec 9, 2013 at 2:29 pm
    So, to clarify, what you want is an easy way to convert something like

    // 24-bits
    type MySignedStruct struct {
        byte [3]byte
    }

    To something like

    // 32-bits
    type MyBiggerSignedStruct struct {
       bytes [4]byte
    }

    (Byte arrays used for simplicity of finding structs of the correct size),
    while maintaining "signedness"? I'm not aware of any way to do that in go
    without writing the logic yourself. You may be able to use unsafe.Pointer
    shenanigans, but, well, unsafe.
    On Monday, December 9, 2013 7:13:40 AM UTC-7, matt wrote:

    No, that looks entirely unrelated, but thanks :)


    On Monday, 9 December 2013 13:42:33 UTC, Jan Mercl wrote:
    On Mon, Dec 9, 2013 at 2:28 PM, matt wrote:
    Is the only way to sign extend a value through use of casting between uint
    and int?

    e.g. if I want to sign-extend from the 16 bit boundary do I need to do the
    following?

    val := uint32(0x0000ffff)
    val = uint32(int32(val << 16) >> 16)
    Do you perhaps mean something like http://play.golang.org/p/PshmCsuvQY
    or http://play.golang.org/p/1RY3dtM3HM ?

    -j

    PS: Go has no casts.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 9, '13 at 1:28p
activeDec 9, '13 at 2:48p
posts8
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase