Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Bitwise operation
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Arithmetic shift=== {{main|Arithmetic shift}} [[File:Rotate left logically.svg|thumb|150px|Left arithmetic shift]] [[File:Rotate right arithmetically.svg|thumb|150px|Right arithmetic shift]] In an ''arithmetic shift'' (sticky shift), the bits that are shifted out of either end are discarded. In a left arithmetic shift, zeros are shifted in on the right; in a right arithmetic shift, the [[sign bit]] (the MSB in two's complement) is shifted in on the left, thus preserving the sign of the operand. This example uses an 8-bit register, interpreted as two's complement: 00010111 (decimal +23) LEFT-SHIFT = 0010111'''0''' (decimal +46) 10010111 (decimal β105) RIGHT-SHIFT = '''1'''1001011 (decimal β53) In the first case, the leftmost digit was shifted past the end of the register, and a new 0 was shifted into the rightmost position. In the second case, the rightmost 1 was shifted out (perhaps into the [[carry flag]]), and a new 1 was copied into the leftmost position, preserving the sign of the number. Multiple shifts are sometimes shortened to a single shift by some number of digits. For example: 00010111 (decimal +23) LEFT-SHIFT-BY-TWO = 010111'''00''' (decimal +92) A left arithmetic shift by ''n'' is equivalent to multiplying by 2<sup>''n''</sup> (provided the value does not [[arithmetic overflow|overflow]]), while a right arithmetic shift by ''n'' of a [[two's complement]] value is equivalent to taking the [[floor and ceiling functions|floor]] of division by 2<sup>''n''</sup>. If the binary number is treated as [[ones' complement]], then the same right-shift operation results in division by 2<sup>''n''</sup> and [[rounding#Round half towards zero|rounding toward zero]].
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)