The operators <<and >>are bit shift operators and can only be applied to integers:
<<would add as many bits (with value 0) to the right of the number as indicated by the value that comes after the operator. Or what is the same: all the bits of the number would be shifted to the left as many positions as specified.
And since a picture is worth a thousand words, here is a graphic explanation created by CBurnett (source: Wikipedia )
>>would remove as many bits to the right of the number as indicated by the value that comes after the operator. Or what is the same: the bits of the number would shift to the right as many positions as specified (truncating the same number of times).
Another graphical explanation created by Cburnett (source: Wikipedia )
The shift operators shift the left operand by the shift count specified by the right operand. They implement arithmetic shifts if the left operand is a signed integer and logical shifts if it is an unsigned integer. There is no upper limit on the shift count. Shifts behave as if the left operand is shifted n times by 1 for a shift count of n. As a result, x << 1 is the same as x*2 and x >> 1 is the same as x/2 but truncated towards negative infinity.
And the Spanish translation (something free, I hope it is understood):
Shift operators shift the left operand as many times as indicated by the right operand. They implement arithmetic shift if the left operand is a signed integer, and logical shift if it is an unsigned integer. There is no limit in terms of the value of displacements to be carried out. Shifts behave as if the left operand is shifted n times by 1 for each shift of n. As a result, x << 1 is the same as x*2 and x >> 1 is the same as x/2 but truncated to negative infinity.
That last part is very important because it allows you to create a formula that will surely help you better understand what they do:
The operator n << mis equivalent to "n multiplied by 2, m times" = n * 2 m .
The operator n >> mis equivalent to "n divided by 2, m times" = n * 2 -m .
Before we start explaining how bitwise shift operators work, let's remember how we get a decimal value from a binary number.
For example, what would be the value of 1011 0011in decimal? Well, according to its position from right to left we have:
Therefore, it 1011 0011is equal to 179in decimal
It is very important to comment that this operator exists in other languages besides GO, such as: Java, php, C, C#, etc.
A simple example to explain how the left bit shift operator works would be:
3 << 2whose value turns out to be 12, the explanation is:
3in binary is 0011the operation indicates a bit shift by 2 positions to the left, therefore:
1100which in binary turns out to be12
If we perform the reverse operation:
12 >> 2whose result is 3;
12in binary it is 1100, if we carry out the bit shift to the right 2 positions, we obtain 0011that in decimal it is 3.
This is an example in Go language where you can see how from the value 1(Value in binary: 0001) when performing a bit shift to the left, its value changes:
package main
import "fmt"
func main() {
var t , i uint
t , i = 1 , 1
for i = 1 ; i < 10 ; i++ {
fmt.Printf("%d << %d = %d \n", t , i , t<<i)
}
}
this would be the output:
1 << 1 = 2 (Valor en binario: 0010)
1 << 2 = 4 (Valor en binario: 0100)
1 << 3 = 8 (Valor en binario: 1000)
1 << 4 = 16 (Valor en binario: 1 0000)
1 << 5 = 32 (Valor en binario: 10 0000)
1 << 6 = 64 (Valor en binario: 100 0000)
1 << 7 = 128 (Valor en binario: 1000 0000)
1 << 8 = 256 (Valor en binario: 1 0000 0000)
1 << 9 = 512 (Valor en binario: 10 0000 0000)
Now an example in Go language performing the bit shift to the right:
package main
import "fmt"
func main() {
var t , i uint
t , i = 1024 , 1
for i = 1 ; i < 10 ; i++ {
fmt.Printf("%d >> %d = %d \n", t , i , t>>i)
}
}
Departure:
1024 >> 1 = 512 (Valor en binario: 10 0000 0000)
1024 >> 2 = 256 (Valor en binario: 1 0000 0000)
1024 >> 3 = 128 (Valor en binario: 1000 0000)
1024 >> 4 = 64 (Valor en binario: 100 0000)
1024 >> 5 = 32 (Valor en binario: 10 0000)
1024 >> 6 = 16 (Valor en binario: 1 0000)
1024 >> 7 = 8 (Valor en binario: 1000)
1024 >> 8 = 4 (Valor en binario: 0100)
1024 >> 9 = 2 (Valor en binario: 0010)
The operators
<<
and>>
are bit shift operators and can only be applied to integers:<<
would add as many bits (with value 0) to the right of the number as indicated by the value that comes after the operator. Or what is the same: all the bits of the number would be shifted to the left as many positions as specified.And since a picture is worth a thousand words, here is a graphic explanation created by CBurnett (source: Wikipedia )
>>
would remove as many bits to the right of the number as indicated by the value that comes after the operator. Or what is the same: the bits of the number would shift to the right as many positions as specified (truncating the same number of times).Another graphical explanation created by Cburnett (source: Wikipedia )
According to the Go documentation on arithmetic operators:
And the Spanish translation (something free, I hope it is understood):
That last part is very important because it allows you to create a formula that will surely help you better understand what they do:
n << m
is equivalent to "n multiplied by 2, m times" = n * 2 m .n >> m
is equivalent to "n divided by 2, m times" = n * 2 -m .Thus, in the case you give as an example:
Big
= 1 * 2 100 = 1,267,650,600,228,229,401,496,703,205,376 (overflows integers).Small
=Big
* 2 -99 = 2 100 * 2 -99 = 2 1 = 2.The << and >> operators are defined as arithmetic operators within the GO language , they are defined as bit shift operators:
<< left bit shift operator.
>> right bit shift operator.
Before we start explaining how bitwise shift operators work, let's remember how we get a decimal value from a binary number.
For example, what would be the value of
1011 0011
in decimal? Well, according to its position from right to left we have:Therefore, it
1011 0011
is equal to179
in decimalIt is very important to comment that this operator exists in other languages besides GO, such as:
Java
,php
,C
,C#
, etc.A simple example to explain how the left bit shift operator works would be:
3 << 2
whose value turns out to be12
, the explanation is:3
in binary is0011
the operation indicates a bit shift by 2 positions to the left, therefore:1100
which in binary turns out to be12
If we perform the reverse operation:
12 >> 2
whose result is3
;12
in binary it is1100
, if we carry out the bit shift to the right 2 positions, we obtain0011
that in decimal it is3
.This is an example in Go language where you can see how from the value
1
(Value in binary:0001
) when performing a bit shift to the left, its value changes:this would be the output:
Now an example in Go language performing the bit shift to the right:
Departure:
More information: https://golang.org/ref/spec#Arithmetic_operators