Knowledge (XXG)

Logical shift

Source 📝

557: 546: 32: 490:. Most C and C++ implementations, and Go, choose which right shift to perform depending on the type of integer being shifted: signed integers are shifted using the arithmetic shift, and unsigned integers are shifted using the logical shift. In particular, C++ uses its logical shift operators as part of the syntax of its input and output functions, called "cin" and "cout" respectively. 493:
All currently relevant C standards (ISO/IEC 9899:1999 to 2011) leave a definition gap for cases where the number of shifts is equal to or bigger than the number of bits in the operands in a way that the result is undefined. This helps allow C compilers to emit efficient code for various platforms by
168:(mantissa); every bit in the operand is simply moved a given number of bit positions, and the vacant bit-positions are filled, usually with zeros, and possibly ones (contrast with a 693: 669: 723: 115: 842: 526: 300: 53: 49: 96: 364: 356: 249: 68: 283: 232: 449: 275: 253: 75: 494:
allowing direct use of the native shift instructions which have differing behavior. For example, shift-left-word in
837: 199: 753: 42: 483: 228: 585: 475: 271: 220: 82: 701: 445: 64: 498:
chooses the more-intuitive behavior where shifting by the bit width or above gives zero, whereas SHL in
360: 472: 433:
bits on a signed or unsigned binary number has the effect of multiplying it by 2. Shifting right by
148:. This is further modulated by the number of bit positions a given value shall be shifted, such as 425:
Logical shifts can be useful as efficient ways to perform multiplication or division of unsigned
20: 525:(LLVM). Others choose to specify the behavior of their most common target platforms, such as 390: 137: 731: 444:
Logical right shift differs from arithmetic right shift. Thus, many languages have different
157: 129: 537:
If the bit sequence 0001 0111 (decimal 23) is logically shifted by one bit position, then:
89: 805: 556: 510: 169: 545: 831: 352: 779: 645: 160:, a logical shift does not preserve a number's sign bit or distinguish a number's 621: 237: 165: 31: 453: 368: 279: 216: 176: 161: 183:
Logical shift operators in various programming languages and processors
495: 441:
binary number has the effect of dividing it by 2 (rounding towards 0).
426: 407: 318: 241: 140:
that shifts all the bits of its operand. The two base variants are the
468:), because left shift via logic and arithmetic have the same effect.) 175:
A logical shift is often used when its operand is being treated as a
19:"shift left" redirects here. For the software testing practice, see 479: 224: 555: 544: 335: 506:, and as such a shift by the bit width doesn't change the value. 514: 386: 670:"Operator Expressions: Arithmetic and Logical Binary Operators" 603: 568:
Note: MSB = Most Significant Bit, LSB = Least Significant Bit
499: 245: 25: 504:
to reduce the maximum execution time of the instructions
604:"Set - Environment Variable - Windows CMD - SS64.com" 502:chooses to mask the shift amount to the lower bits 56:. Unsourced material may be challenged and removed. 780:"LLVM Language Reference Manual - shl Instruction" 517:, also leave shifting by the bit width and above 486:, however, have only one right shift operator, 460:, but the arithmetic right shift operator is 8: 554:Shift right yields: 0000 1011 (decimal 11) 543:Shift left yields: 0010 1110 (decimal 46) 464:. (Java has only one left shift operator ( 116:Learn how and when to remove this message 181: 577: 456:, the logical right shift operator is 385:VHSIC Hardware Description Language ( 7: 54:adding citations to reliable sources 429:by powers of two. Shifting left by 806:"<< Operator (C# Reference)" 529:which specifies the x86 behavior. 14: 622:"BitwiseOperators - Python Wiki" 179:of bits instead of as a number. 30: 724:"x86 Instruction Set Reference" 41:needs additional citations for 694:"PowerPC Instruction Set: slw" 1: 509:Some languages, such as the 16:Bit-level computer operation 560:Logical right shift one bit 859: 549:Logical left shift one bit 448:for them. For example, in 18: 646:"Shl in std::ops - Rust" 586:"The Package Interfaces" 843:Operators (programming) 561: 550: 235:(unsigned types only); 559: 548: 473:programming languages 303:(unsigned types only) 256:(unsigned types only) 188:Language or processor 50:improve this article 754:"Opcodes.Shl Field" 734:on 19 February 2018 704:on 4 September 2014 184: 146:logical right shift 810:msdn.microsoft.com 758:msdn.microsoft.com 728:x86.renejeschke.de 698:pds.twi.tudelft.nl 562: 551: 182: 142:logical left shift 21:shift-left testing 838:Binary arithmetic 674:doc.rust-lang.org 650:doc.rust-lang.org 566: 565: 423: 422: 138:bitwise operation 126: 125: 118: 100: 850: 822: 821: 819: 817: 802: 796: 795: 793: 791: 776: 770: 769: 767: 765: 750: 744: 743: 741: 739: 730:. Archived from 720: 714: 713: 711: 709: 700:. Archived from 690: 684: 683: 681: 680: 666: 660: 659: 657: 656: 642: 636: 635: 633: 632: 618: 612: 611: 600: 594: 593: 582: 540: 539: 489: 467: 463: 459: 419: 414: 402: 397: 380: 375: 347: 342: 330: 325: 313: 308: 295: 290: 266: 261: 211: 206: 185: 158:arithmetic shift 154:shift right by n 130:computer science 121: 114: 110: 107: 101: 99: 58: 34: 26: 858: 857: 853: 852: 851: 849: 848: 847: 828: 827: 826: 825: 815: 813: 804: 803: 799: 789: 787: 778: 777: 773: 763: 761: 752: 751: 747: 737: 735: 722: 721: 717: 707: 705: 692: 691: 687: 678: 676: 668: 667: 663: 654: 652: 644: 643: 639: 630: 628: 626:wiki.python.org 620: 619: 615: 602: 601: 597: 584: 583: 579: 574: 535: 487: 465: 461: 457: 417: 412: 400: 395: 378: 373: 345: 340: 328: 323: 311: 306: 293: 288: 264: 259: 236: 209: 204: 150:shift left by 1 122: 111: 105: 102: 65:"Logical shift" 59: 57: 47: 35: 24: 17: 12: 11: 5: 856: 854: 846: 845: 840: 830: 829: 824: 823: 797: 786:. LLVM Project 771: 745: 715: 685: 661: 637: 613: 595: 576: 575: 573: 570: 564: 563: 552: 534: 531: 511:.NET Framework 421: 420: 415: 410: 404: 403: 398: 393: 382: 381: 376: 371: 349: 348: 343: 338: 332: 331: 326: 321: 315: 314: 309: 304: 297: 296: 291: 286: 268: 267: 262: 257: 213: 212: 207: 202: 196: 195: 192: 189: 170:circular shift 124: 123: 38: 36: 29: 15: 13: 10: 9: 6: 4: 3: 2: 855: 844: 841: 839: 836: 835: 833: 811: 807: 801: 798: 785: 781: 775: 772: 759: 755: 749: 746: 733: 729: 725: 719: 716: 703: 699: 695: 689: 686: 675: 671: 665: 662: 651: 647: 641: 638: 627: 623: 617: 614: 609: 605: 599: 596: 591: 590:www.adaic.org 587: 581: 578: 571: 569: 558: 553: 547: 542: 541: 538: 532: 530: 528: 524: 520: 516: 512: 507: 505: 501: 497: 491: 485: 481: 477: 474: 469: 455: 451: 447: 442: 440: 436: 432: 428: 416: 411: 409: 406: 405: 399: 394: 392: 388: 384: 383: 377: 372: 370: 366: 362: 358: 354: 353:Object Pascal 351: 350: 344: 339: 337: 334: 333: 327: 322: 320: 317: 316: 310: 305: 302: 299: 298: 294:>>> 292: 287: 285: 281: 277: 273: 270: 269: 263: 258: 255: 251: 247: 243: 239: 234: 230: 226: 222: 218: 215: 214: 208: 203: 201: 198: 197: 193: 190: 187: 186: 180: 178: 173: 171: 167: 163: 159: 155: 151: 147: 143: 139: 135: 134:logical shift 131: 120: 117: 109: 98: 95: 91: 88: 84: 81: 77: 74: 70: 67: –  66: 62: 61:Find sources: 55: 51: 45: 44: 39:This article 37: 33: 28: 27: 22: 814:. Retrieved 809: 800: 788:. Retrieved 783: 774: 762:. Retrieved 757: 748: 736:. Retrieved 732:the original 727: 718: 706:. Retrieved 702:the original 697: 688: 677:. Retrieved 673: 664: 653:. Retrieved 649: 640: 629:. Retrieved 625: 616: 607: 598: 589: 580: 567: 536: 522: 518: 508: 503: 492: 470: 458:>>> 443: 438: 434: 430: 424: 361:x86 assembly 312:>>> 307:<<< 174: 156:. Unlike an 153: 149: 145: 141: 133: 127: 112: 103: 93: 86: 79: 72: 60: 48:Please help 43:verification 40: 812:. Microsoft 760:. Microsoft 519:unspecified 437:bits on an 238:Standard ML 210:Shift_Right 166:significand 832:Categories 679:2022-11-13 655:2022-01-17 631:2018-01-24 572:References 521:(.NET) or 454:JavaScript 369:Powershell 280:JavaScript 205:Shift_Left 106:April 2016 76:newspapers 523:undefined 446:operators 289:<< 265:>> 260:<< 164:from its 784:llvm.org 608:ss64.com 488:>> 466:<< 462:>> 439:unsigned 427:integers 177:sequence 162:exponent 144:and the 816:9 April 790:9 April 764:9 April 738:9 April 708:9 April 533:Example 496:PowerPC 408:PowerPC 319:Fortran 242:Verilog 90:scholar 482:, and 365:Kotlin 357:Delphi 329:RSHIFT 324:LSHIFT 250:Python 194:Right 92:  85:  78:  71:  63:  336:OCaml 284:Julia 233:Swift 217:Batch 136:is a 97:JSTOR 83:books 818:2016 792:2016 766:2016 740:2016 710:2016 515:LLVM 513:and 471:The 452:and 450:Java 391:MIPS 387:VHDL 276:Java 254:Rust 191:Left 132:, a 69:news 500:x86 480:C++ 418:srw 413:slw 401:srl 396:sll 389:), 379:shr 374:shl 346:lsr 341:lsl 246:PHP 225:C++ 200:Ada 172:). 152:or 128:In 52:by 834:: 808:. 782:. 756:. 726:. 696:. 672:. 648:. 624:. 606:. 588:. 527:C# 484:Go 478:, 367:, 363:, 359:, 355:, 301:F# 282:, 278:, 274:, 252:, 248:, 244:, 240:, 231:, 229:Go 227:, 223:, 219:, 820:. 794:. 768:. 742:. 712:. 682:. 658:. 634:. 610:. 592:. 476:C 435:n 431:n 272:D 221:C 119:) 113:( 108:) 104:( 94:· 87:· 80:· 73:· 46:. 23:.

Index

shift-left testing

verification
improve this article
adding citations to reliable sources
"Logical shift"
news
newspapers
books
scholar
JSTOR
Learn how and when to remove this message
computer science
bitwise operation
arithmetic shift
exponent
significand
circular shift
sequence
Ada
Batch
C
C++
Go
Swift
Standard ML
Verilog
PHP
Python
Rust

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.