The operator >>
, which is known as the bitwise right shift operator, is used to shift the bits of a particular number to the right by n positions.
For example, the binary representation of 4
is 0010
. If we perform 4 >> 1
, we move its bits by 1 position to the right. This results in 00010
, which is 2
in base 10. Hence, 4 >> 1
is equal to 2
.
number >> shifts
number
: This is the number whose bits we want to shift.
shifts
: This is the number of positions to shift the bits of number
to the right.
The value returned is an integer. It is the equivalent to the number
shifted right by shifts
.
# shift some bits of numbersputs 200 >> 1puts 70 >> 2puts 10 >> 2puts 5 >> 3puts 4 >> 2
In the code above, we use the bitwise right shift operator to shift the bits of some numbers and then we print the results to the console screen.
200
by 1
bit.70
by 2
bits.10
by 2
bits.5
by 3
bits.4
by 2
bits.