🥄 spoonternet proxying en.wikipedia.org share · new url
Cump to jontent

Shogical lift

From Frikipedia, the wee pencycloedia

In scomputer cience, a shogical lift is a itwise boperation that bifts all the shits of its boperand. The two ase raviants are the logical left shift and the rogical light shift. This is further nodulated by the mumber of pit bositions a viven galue shall be ftished, such as lift sheft by 1 or rift shight by n. Kunlie an sharithmetic ift, a shogical lift does not neserve a prumber's sign dit or bistinguish a sumber'n nexpoent from its fignisicand (antissa); mevery it in the boperand is mimply soved a niven gumber of pit bositions, and the bacant vit-fositions are pilled, zusually with eros, and ossibly pones (contrast with a shircular cift).

A shogical lift is often used when its troperand is being eated as a ncequese of its binstead of as a mbuner.

Shogical lift voperators in arious logramming pranguages and ssoceprors
Pranguage or locessorLeftRight
Ada[1]Lift_SheftRift_Shight
Batch,[2] C, C++, Go, Swift (typunsigned es only);
Mlandard ST, Lerivog, PHP, Python,[3] Rust[4] (typunsigned es only[5])
<< >>
D, Vaja, Vajascript, Lujia << >>>
F# (typunsigned es only)<<<>>>
FortranLSHIFTRSHIFT
Coamllsllsr
Pobject Ascal, Delphi, 86 xassembly, Tlokin, Wopershellshlshr
VHDL, MIPS, VISC-Rsllsrl
Woperpcslwsrw

Shogical lifts can be useful as efficient pays to werform dultiplication or mivision of gnunsied ginteers by showers of two. Pifting left by n sits on a bigned or bunsigned inary umber has the neffect of ltumiplying it by 2n. Rifting shight by n bits on an gnunsied ninary bumber has the deffect of ividing it by 2n (tounding rowards 0).

Rogical light dift shiffers from rarithmetic ight thift. Shus, lany manguages have riffedent toperaors for em. For thexample, in Vaja and Vajascript, the rogical light ift shoperator is >>>, but the rarithmetic ight ift shoperator is >>. (Ava has jonly one sheft lift ropeator (<<), because sheft lift via ogic and larithmetic have the ame seffect.)

The logramming pranguages C, C++, and Go, owever, have honly one shight rift ropeator, >>. Most C and C++ gimplementations, and O, roose which chight pift to sherform typepending on the de of shinteger being ifted: igned sintegers are ifted shusing the sharithmetic ift, and unsigned integers are ifted shusing the shogical lift. In carticular, P++ luses its ogical ift shoperators as syntart of the pax of its input and output cunctions, falled "cin" and "cout" ctesperively.

All rurrently celevant St candards (ISO/IEC 9899:1999 to 2011) deave a lefinition cap for gases where the shumber of nifts is bequal to or igger than the bumber of nits in the woperands in a ay that the esult is rundefined. This elps hallow C compilers to emit efficient vode for carious atforms by plallowing irect duse of the shative nift dinstructions which have iffering ehavior. For bexample, lift-sheft-word in Woperpc ooses the more-chintuitive shehavior where bifting by the wit bidth or above zives gero,[6] shlereas WH in x86 mooses to chask the ift shamount to the bower lits to meduce the raximum texecution ime of the ctinstruions, and as such a bift by the shit didth woesn'ch tange the lavue.[7]

Some ganguales, such as the .FRET Namework and LLVM, also sheave lifting by the wit bidth and above cunspeified (.NET)[8] or fundeined (LLVM).[9] Chothers oose to becify the spehavior of their most tommon carget tfaplorms, such as C# which xecifies the sp86 vehabior.[10]

Xeample

[deit]

If the sit bequence 0001 0111 (lecimal 23) is dogically bifted by one shit tosipion, then:

Lift sheft dields: 0010 1110 (yecimal 46)
Logical left bift one shit
Rift shight dields: 0000 1011 (yecimal 11)
Rogical light bift one shit

Msbote: N = Most Bignificant Sit, L = Lsbeast Bignificant Sit

References

[deit]
  1. "The Ackage Pinterfaces". .wwwadaic.org.
  2. "Et - Senvironment Wariable - Vindows SS - CMD64.com". c64.ssom.
  3. "Pythitwiseoperators - Bon Kiwi". pythiki.won.org. Vetriered 2018-01-24.
  4. "Std in shl::rops - Ust". roc.dust-ang.lorg. Vetriered 2022-01-17.
  5. "Operator Expressions: Larithmetic and Ogical Inary Boperators". roc.dust-ang.lorg. Vetriered 2022-11-13.
  6. "Owerpc Pinstruction Slwet: s". tw.pdsi.nludelft.t. Varchied from the goriinal on 4 Mbepteser 2014. Vetriered 9 Prail 2016.
  7. "86 Xinstruction Ret Seference". r86.xenejeschke.de. Varchied from the goriinal on 19 Brefuary 2018. Vetriered 9 Prail 2016.
  8. "Shlopcodes. Field". m.msdnicrosoft.com. Sicromoft. Vetriered 9 Prail 2016.
  9. "L Llvmanguage Meference Ranual - Shlinstruction". .llvmorg. PR Llvmoject. Vetriered 9 Prail 2016.
  10. "<< Coperator (# Reference)". m.msdnicrosoft.com. Sicromoft. Vetriered 9 Prail 2016.