FAQ
Let's assume I have a structure in c++.
typedef union
{
     struct {
         unsigned part_a:2;
         unsigned part_b:6;
         unsigned part_c:24;
     } bits;
     uint32_t value;
} loc_t;

to set part_b value:
loc.bits.part_b=x;

But in golang to access fields in simple way we need in the following.
type loc_t uint32;
func (this *loc_t) set_part_a(x byte) {
     x = x & 3
     this = this &^ 3
     this |= x
}

with "part_b" it is a little bit more sophisticated.
Anyway we need to provide setters/getters to manipulate with bits, it is
kind of OK, but not cool.
Maybe I miss something, but i feel lack of bits manipulation in the golang.

--
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

  • Jeremy Wall at Sep 19, 2013 at 2:25 pm

    On Thu, Sep 19, 2013 at 6:37 AM, Yurij Safronov wrote:

    Let's assume I have a structure in c++.
    typedef union
    {
    struct {
    unsigned part_a:2;
    unsigned part_b:6;
    unsigned part_c:24;
    } bits;
    uint32_t value;
    } loc_t;

    to set part_b value:
    loc.bits.part_b=x;

    But in golang to access fields in simple way we need in the following.
    type loc_t uint32;
    func (this *loc_t) set_part_a(x byte) {
    x = x & 3
    this = this &^ 3
    this |= x
    }

    with "part_b" it is a little bit more sophisticated.
    Anyway we need to provide setters/getters to manipulate with bits, it is
    kind of OK, but not cool.
    Maybe I miss something, but i feel lack of bits manipulation in the golang.
    I'm not following. Are you talking about cgo access to a struct?
    --
    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.
    --
    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.
  • Yurij Safronov at Sep 19, 2013 at 2:34 pm
    No. I suggest to introduce bit type for golang, like "unsigned part_a:2;".
    Or to invent something to make bit manipulations easier.

    --
    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.
  • Minux at Sep 19, 2013 at 2:41 pm

    On Sep 19, 2013 10:34 AM, "Yurij Safronov" wrote:
    No. I suggest to introduce bit type for golang, like "unsigned
    part_a:2;". Or to invent something to make bit manipulations easier.
    bit fields are very nonportable, so i think it is against the philosophy of
    Go.

    for example, see how portable C programs (linux kernel, for example)
    surround structs with bitfields in with ifdef LITTLE_ENDIAN or
    the like, and some portable C guidelines prohibit using bit fields
    altogether.

    --
    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.
  • Yurij Safronov at Sep 19, 2013 at 2:55 pm
    Everyone who accept to work with bits are also accept to care about
    endianness and portability at some point. At other hand, in server
    programming it is better to accept such issues rather than have 8TB vs. 1TB.
    On Thursday, September 19, 2013 5:41:15 PM UTC+3, minux wrote:

    On Sep 19, 2013 10:34 AM, "Yurij Safronov" wrote:
    No. I suggest to introduce bit type for golang, like "unsigned
    part_a:2;". Or to invent something to make bit manipulations easier.
    bit fields are very nonportable, so i think it is against the philosophy
    of Go.

    for example, see how portable C programs (linux kernel, for example)
    surround structs with bitfields in with ifdef LITTLE_ENDIAN or
    the like, and some portable C guidelines prohibit using bit fields
    altogether.
    --
    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.
  • Minux at Sep 19, 2013 at 5:35 pm

    On Sep 19, 2013 10:55 AM, "Yurij Safronov" wrote:
    Everyone who accept to work with bits are also accept to care about
    endianness and portability at some point. At other hand, in server
    programming it is better to accept such issues rather than have 8TB vs. 1TB.
    just define accessing methods on the struct, and if property written,
    should be able to be inlined, so hopefully no performance difference to
    that of C.
    On Thursday, September 19, 2013 5:41:15 PM UTC+3, minux wrote:
    On Sep 19, 2013 10:34 AM, "Yurij Safronov" wrote:
    No. I suggest to introduce bit type for golang, like "unsigned
    part_a:2;". Or to invent something to make bit manipulations easier.
    bit fields are very nonportable, so i think it is against the philosophy
    of Go.
    for example, see how portable C programs (linux kernel, for example)
    surround structs with bitfields in with ifdef LITTLE_ENDIAN or
    the like, and some portable C guidelines prohibit using bit fields
    altogether.
    --
    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.
    --
    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.
  • Arie van Wingerden at Sep 19, 2013 at 2:57 pm
    Have a look at the beautiful way this (even endianness) is solved in Erlang!


    2013/9/19 minux <minux.ma@gmail.com>
    On Sep 19, 2013 10:34 AM, "Yurij Safronov" wrote:
    No. I suggest to introduce bit type for golang, like "unsigned
    part_a:2;". Or to invent something to make bit manipulations easier.
    bit fields are very nonportable, so i think it is against the philosophy
    of Go.

    for example, see how portable C programs (linux kernel, for example)
    surround structs with bitfields in with ifdef LITTLE_ENDIAN or
    the like, and some portable C guidelines prohibit using bit fields
    altogether.

    --
    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.
    --
    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
postedSep 19, '13 at 1:55p
activeSep 19, '13 at 5:35p
posts7
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase