FAQ
Hi all,

As someone who came to Go from python (as opposed to C) I never really had
used bit manipulation before. Now I find myself in need of it.

These are my questions:

1. How can I easily set a bit in a byte of a []byte?
2. How can I easily test a bit in a byte of a []byte?
3. How can I easily set a 16bit/24/32/anything []byte to be the value of a
number? (for instance, if a spec says bytes 3-4 are an unsigned/signed
int16/24/32/whatever)

I know I have to use the << (or is it >>) operator, but I don't quite
understand.


Thanks for your help :),

Tom

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

  • Eric Gavaletz at Oct 3, 2013 at 1:00 am
    This is the best collection I have come across. The examples are given in
    C, but port easily to Go.

    http://graphics.stanford.edu/~seander/bithacks.html

    On Wednesday, October 2, 2013 7:58:19 PM UTC-4, Tom D wrote:

    Hi all,

    As someone who came to Go from python (as opposed to C) I never really had
    used bit manipulation before. Now I find myself in need of it.

    These are my questions:

    1. How can I easily set a bit in a byte of a []byte?
    2. How can I easily test a bit in a byte of a []byte?
    3. How can I easily set a 16bit/24/32/anything []byte to be the value of a
    number? (for instance, if a spec says bytes 3-4 are an unsigned/signed
    int16/24/32/whatever)

    I know I have to use the << (or is it >>) operator, but I don't quite
    understand.


    Thanks for your help :),

    Tom
    --
    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.
  • Michael Jones at Oct 3, 2013 at 1:03 am
    Go bit operations echo C.

    Tutorials:
    http://www.cprogramming.com/tutorial/bitwise_operators.html
    http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/BitOp/bitwise.html
    http://en.wikipedia.org/wiki/Bitwise_operations_in_C


    On Wed, Oct 2, 2013 at 6:00 PM, Eric Gavaletz wrote:

    This is the best collection I have come across. The examples are given in
    C, but port easily to Go.

    http://graphics.stanford.edu/~seander/bithacks.html

    On Wednesday, October 2, 2013 7:58:19 PM UTC-4, Tom D wrote:

    Hi all,

    As someone who came to Go from python (as opposed to C) I never really
    had used bit manipulation before. Now I find myself in need of it.

    These are my questions:

    1. How can I easily set a bit in a byte of a []byte?
    2. How can I easily test a bit in a byte of a []byte?
    3. How can I easily set a 16bit/24/32/anything []byte to be the value of
    a number? (for instance, if a spec says bytes 3-4 are an unsigned/signed
    int16/24/32/whatever)

    I know I have to use the << (or is it >>) operator, but I don't quite
    understand.


    Thanks for your help :),

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


    --
    Michael T. Jones | Chief Technology Advocate | mtj@google.com | +1
    650-335-5765

    --
    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.
  • Rob Pike at Oct 3, 2013 at 5:56 am
    Go has the &^ bit clear operator, which C does not.

    -rob

    --
    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.
  • Michael Jones at Oct 4, 2013 at 11:10 pm
    ...and neither have <<< and >>> (or some other) for rotations.

    x = x <<< 17

    is clearer than

    c = (x << 17) | (x >> 47)
    or
    c = (x << 17) | (x >> 15)

    as the case may be.

    On Wed, Oct 2, 2013 at 10:56 PM, Rob Pike wrote:

    Go has the &^ bit clear operator, which C does not.

    -rob

    --
    Michael T. Jones | Chief Technology Advocate | mtj@google.com | +1
    650-335-5765

    --
    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
postedOct 2, '13 at 11:58p
activeOct 4, '13 at 11:10p
posts5
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase