Home

Shiftright

Shiftright is a bitwise operation that moves each bit in the binary representation of a value to the right by a specified number of positions. The bits shifted out on the right are discarded, and the bits on the left are filled with zeros in logical shifts, or with the sign bit in arithmetic shifts. In many programming languages two related operators exist: a logical shift right and an arithmetic shift right; the symbols and exact rules vary.

For example, in Java, the operator >> performs an arithmetic shift (sign-extended for signed values), while >>> performs

Common examples: 0b11010100 >> 3 equals 0b00011010 (decimal 212 >> 3 = 26). For unsigned numbers the result is

Uses: shiftright is an efficient way to divide by powers of two, perform bit masking and bit-flag

Shiftright contrasts with left shift, which moves bits toward higher-order positions. Some languages also offer an

a
logical
shift
that
fills
with
zeros.
In
C
and
C++,
the
result
of
shifting
a
signed
negative
value
is
implementation-defined,
and
shifting
by
an
amount
greater
than
or
equal
to
the
width
of
the
type
is
undefined
behavior
in
C.
Python
uses
an
arithmetic
right
shift
for
integers,
sign-extending
negative
values.
simply
the
floor
of
the
original
divided
by
2^n.
manipulation,
and
handle
low-level
data
processing
such
as
parsing
and
protocol
handling.
In
hardware,
the
operation
is
implemented
by
a
right
shifter
circuit,
and
high-level
languages
map
the
operation
to
CPU
instructions.
unsigned
right
shift
or
allow
rotation
operations,
which
move
bits
around
the
ends
rather
than
discarding
them.