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
Arithmetic shift
(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!
== Formal definition == The formal definition of an arithmetic shift, from [[Federal Standard 1037C]] is that it is: {{bq|text=A shift, applied to the representation of a number in a fixed [[radix]] numeration system and in a [[fixed-point arithmetic|fixed-point]] representation system, and in which only the characters representing the fixed-point part of the number are moved. An arithmetic shift is usually equivalent to multiplying the number by a positive or a negative integral power of the radix, except for the effect of any rounding; compare the [[logical shift]] with the arithmetic shift, especially in the case of [[floating-point]] representation.}} An important word in the FS 1073C definition is "usually". === Non-equivalence of arithmetic right shift and division === However, arithmetic ''right'' shifts are major traps for the unwary, specifically in treating rounding of negative integers. For example, in the usual [[two's complement]] representation of negative integers, β1 is represented as all 1's. For an 8-bit signed integer this is 1111 1111. An arithmetic right-shift by 1 (or 2, 3, ..., 7) yields 1111 1111 again, which is still β1. This corresponds to rounding down (towards negative infinity), but is not the usual convention for division. It is frequently stated that arithmetic right shifts are equivalent to [[division (mathematics)|division]] by a (positive, integral) power of the radix (e.g., a division by a power of 2 for binary numbers), and hence that division by a power of the radix can be optimized by implementing it as an arithmetic right shift. (A shifter is much simpler than a divider. On most processors, shift instructions will execute faster than division instructions.) Large number of 1960s and 1970s programming handbooks, manuals, and other specifications from companies and institutions such as [[Digital Equipment Corporation|DEC]], [[IBM]], [[Data General]], and [[American National Standards Institute|ANSI]] make such incorrect statements{{sfn|Steele|1977|p=}}{{Page needed|date=March 2012}}. Logical right shifts are equivalent to division by a power of the radix (usually 2) only for positive or unsigned numbers. Arithmetic right shifts are equivalent to logical right shifts for positive signed numbers. Arithmetic right shifts for negative numbers in N's complement (usually [[two's complement]]) is roughly equivalent to division by a power of the radix (usually 2), where for odd numbers rounding downwards is applied (not towards 0 as usually expected). Arithmetic right shifts for negative numbers are equivalent to division using rounding towards 0 in [[ones' complement]] representation of signed numbers as was used by some historic computers, but this is no longer in general use. ==== Handling the issue in programming languages ==== The (1999) ISO standard for the programming language [[C (programming language)|C]] defines the right shift operator in terms of divisions by powers of 2.{{sfn|ISOIEC9899|1999|loc=Β§ 6.5.7 Bitwise shift operators}} Because of the above-stated non-equivalence, the standard explicitly excludes from that definition the right shifts of signed numbers that have negative values. It does not specify the behaviour of the right shift operator in such circumstances, but instead requires each individual C compiler to define the behaviour of shifting negative values right.{{#tag:ref|The C standard was intended to not restrict the C language to either ones' complement or two's complement architectures. In cases where the behaviours of ones' complement and two's complement representations differ, such as this, the standard requires individual C compilers to document the behaviour of their target architectures. The documentation for [[GNU Compiler Collection]] (GCC), for example, documents its behaviour as employing sign-extension.{{sfn|FSF|2008|loc=Β§ 4.5 Integers implementation}}|group="note"}} Like C, C++ had an implementation-defined right shift for signed integers until C++20. Starting in the C++20 standard, right shift of a signed integer is defined to be an arithmetic shift.{{sfn|ISOCPP20|2020|loc=Β§ 7.6.7 Shift operators}}
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)