Go << and >> operators

brianoh picture brianoh · Apr 27, 2011 · Viewed 66k times · Source

Could someone please explain to me the usage of << and >> in Go? I guess it is similar to some other languages.

Answer

Peter Oram picture Peter Oram · Apr 21, 2014

The super (possibly over) simplified definition is just that << is used for "times 2" and >> is for "divided by 2" - and the number after it is how many times.

So n << x is "n times 2, x times". And y >> z is "y divided by 2, z times".

For example, 1 << 5 is "1 times 2, 5 times" or 32. And 32 >> 5 is "32 divided by 2, 5 times" or 1.

All the other answers give the more technical definition, but nobody laid it out really bluntly and I thought you might want that.