1

I understand the whole, it shifts the bits over

0000 0000 0000 0000 0000 0000 0000 0111 << 1
0000 0000 0000 0000 0000 0000 0000 1110

But why would you want to use these left and right shift operators instead of just typing out the number, where does using these operators actually have a benefit.

I see a lot of answers on Stackoverflow and what the operator accomplishes, but nobody ever says WHY they would use it over just typing out 12345 so like I said, why use them and what is their benefit over just typing the number out you're trying to get?

I came across this code while browsing a package on github:

// ClientVersion is the protocol version that Client implements.
const ClientVersion = 1<<16 | 3<<8 | 0

Which the number comes out to be: 66304

So if this is a constant why not just type const ClientVersion = 66304 why use the operators.

Datsik
  • 14,453
  • 14
  • 80
  • 121

2 Answers2

1

If you assume a an integer, then a << x multiplies a by 2^x and a >> x divides b by 2^x, where you use an integer division.

In the case that you described I see no real benefit of using 1<<16 | 3<<8 | 0 instead of 66304 (except of show-off that you can use bitwise operators, which in my stupid opinion is stupid).

But there are ways where I think that they are justifiable (take a look at this question about iota constants).

A couple of other examples (not only related to Go):

  • check if n-th bit is set x & (1<<n)
  • set the n-th bit x | (1<<n)
  • many other manipulations with n-th bit.
Community
  • 1
  • 1
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
  • 6
    The benefit is that the programmer can instantly see and maintain the version number (16.3.0). (I'm assuming there are 8-bit fields for each major/minor/patch version). If the version number changes, the compiler handles the computations. The number 66304 tells us nothing, we must parse it to figure out the embedded fields. – BraveNewCurrency Jan 07 '16 at 02:57
  • 1
    Just in case anyone is confused, I'm sure @BraveNewCurrency meant a logical version of (1.3.0), which is a very nice way to have a semver encoded into a single small integer. – JimB Jan 07 '16 at 16:00
0

When you require small memory footprints, being able to use a single byte to hold 256 different options instead of using a big string array or more bytes then you will use these.

Embedded systems make use of bit wise operations to turn on/off options, any system that needs to be somewhat self sustainable and not depend on a rich external datastore would benefit from this technique.

T McKeown
  • 12,971
  • 1
  • 25
  • 32
  • Do people know every bit combination off the top of their head or something? I would have to use some sort of conversion or something to figure out what I needed – Datsik Jan 07 '16 at 02:04
  • @Datsik The basic operations are all you need to learn to be proficient at bitwise operations (or at least, look at them without fear). Basic masking and bit flipping would get you started on a solid foundation for harder stuff. – Simon Whitehead Jan 07 '16 at 05:23
  • @Datsik: these are things all programmers should be familiar with, at least so they can read other's code, like in the stdlib for instance: https://golang.org/src/encoding/binary/binary.go?#L58 . These go hand-in-hand with the other bitwise operations for a whole host of functionality https://graphics.stanford.edu/~seander/bithacks.html. Bitfields and btmasks are very common, and you probably interact with them all the time without even knowing it. – JimB Jan 07 '16 at 14:21