20:
28:
1001:
457:, the SAR instruction (arithmetic right shift) divides a signed number by a power of two, rounding towards negative infinity. However, the IDIV instruction (signed divide) divides a signed number, rounding towards zero. So a SAR instruction cannot be substituted for an IDIV by power of two instruction nor vice versa.
737:
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
655:
macro language, whether an arithmetic shift is left or right is determined by whether the second operand is positive or negative. This is unusual. In most programming languages the two directions have distinct operators, with the operator specifying the direction, and the second operand is implicitly
595:
raster coordinates by a power of two, which maintains even spacing. For example, right shift by 1 sends 0, 1, 2, 3, 4, 5, ... to 0, 0, 1, 1, 2, 2, ..., and โ1, โ2, โ3, โ4, ... to โ1, โ1, โ2, โ2, ..., maintaining even spacing as โ2, โ2, โ1, โ1, 0, 0, 1, 1, 2, 2, ... In contrast, integer division with
527:
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
719:
The VHDL arithmetic left shift operator is unusual. Instead of filling the LSB of the result with zero, it copies the original LSB into the new LSB. While this is an exact mirror image of the arithmetic right shift, it is not the conventional definition of the operator, and is not equivalent to
579:
defines the right shift operator in terms of divisions by powers of 2. 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
535:
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
481:
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
555:
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
449:
binary number has the effect of dividing it by 2, but it always rounds down (towards negative infinity). This is different from the way rounding is usually done in signed integer division (which rounds towards 0). This discrepancy has led to bugs in a number of compilers.
506:
shifts are equivalent to multiplication by a (positive, integral) power of the radix (e.g., a multiplication by a power of 2 for binary numbers). Logical left shifts are also equivalent, except multiplication and arithmetic shifts may trigger
720:
multiplication by a power of 2. In the VHDL 2008 standard this strange behavior was left unchanged (for backward compatibility) for argument types that do not have forced numeric interpretation (e.g., BIT_VECTOR) but 'SLA' for
615:
operator in C and C++ is not necessarily an arithmetic shift. Usually it is only an arithmetic shift if used with a signed integer type on its left-hand side. If it is used on an unsigned integer type instead, it will be a
407:
that shifts all of the bits of its operand; every bit in the operand is simply moved a given number of bit positions, and the vacant bit-positions are filled in. Instead of being filled with all 0s, as in
656:
positive. (Some languages, such as
Verilog, require that negative values be converted to unsigned positive values. Some languages, such as C and C++, have no defined behaviour if negative values are used.)
728:
argument types behaves in the expected way (i.e., rightmost positions are filled with zeros). VHDL's shift left logical (SLL) function does implement the aforementioned 'standard' arithmetic shift.
1005:
583:
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.
638:
The
Verilog arithmetic right shift operator only actually performs an arithmetic shift if the first operand is signed. If the first operand is unsigned, the operator actually performs a
536:
will execute faster than division instructions.) Large number of 1960s and 1970s programming handbooks, manuals, and other specifications from companies and institutions such as
867:
1142:
1093:
560:) 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).
596:
rounding towards zero sends โ1, 0, and 1 all to 0 (3 points instead of 2), yielding โ2, โ1, โ1, 0, 0, 0, 1, 1, 2, 2, ... instead, which is irregular at 0.
710:; for new definitions the programmer need provide only one of the two forms and the other form will be automatically defined in terms of the provided one.
1133:
669:
can be both left and right shift, depending on the second operand, very similar to the OpenVMS macro language, although R6RS Scheme adds both
434:
Arithmetic shifts can be useful as efficient ways to perform multiplication or division of signed integers by powers of two. Shifting left by
790:
912:
23:
A right arithmetic shift of a binary number by 1. The empty position in the most significant bit is filled with a copy of the original MSB.
854:
891:
1163:
1148:
591:
In applications where consistent rounding down is desired, arithmetic right shifts for signed values are useful. An example is in
580:
such circumstances, but instead requires each individual C compiler to define the behaviour of shifting negative values right.
1010:
1047:
549:
523:
shifts are major traps for the unwary, specifically in treating rounding of negative integers. For example, in the usual
1101:
1205:
1036:
1016:
814:
567:
representation of signed numbers as was used by some historic computers, but this is no longer in general use.
265:
92:
537:
224:
145:
68:
110:
100:
105:
76:
60:
416:
in signed integer representations) is replicated to fill in all the vacant positions (this is a kind of
1200:
128:
96:
1121:
563:
Arithmetic right shifts for negative numbers are equivalent to division using rounding towards 0 in
438:
bits on a signed or unsigned binary number has the effect of multiplying it by 2. Shifting right by
1171:
576:
454:
88:
80:
905:
739:
384:
532:
478:
466:
400:
331:
313:
32:
376:
8:
564:
557:
524:
508:
443:
1109:
1077:
766:
497:
954:
404:
1081:
1067:
1059:
1020:
1085:
1141:
Hyde, Randall (1996-09-26). "CHAPTER SIX: THE 80x86 INSTRUCTION SET (Part 3)".
487:
417:
391:(though it is not restricted to signed operands). The two basic types are the
19:
1194:
483:
409:
1179:
966:
1031:
978:
545:
56:
40:
Arithmetic shift operators in various programming languages and processors
31:
A left arithmetic shift of a binary number by 1. The empty position in the
1063:
894:. Section 8.1: "Sticky versus Non-Sticky Bit-shifting". Cryptologia. 1997.
592:
514:
236:
179:
742:(GCC), for example, documents its behaviour as employing sign-extension.
27:
1072:
840:
707:
531:
It is frequently stated that arithmetic right shifts are equivalent to
64:
498:
Equivalence of arithmetic and logical left shifts and multiplication
413:
1100:. Hewlett-Packard Development Company. April 2001. Archived from
652:
213:
196:
162:
862:
855:"The RISC-V Instruction Set Manual, Volume I: Unprivileged ISA"
791:"Operator Expressions: Arithmetic and Logical Binary Operators"
349:
570:
473:
A shift, applied to the representation of a number in a fixed
930:
474:
248:
84:
412:, when shifting to the right, the leftmost bit (usually the
494:
An important word in the FS 1073C definition is "usually".
277:
541:
295:
72:
1185:. International Organization for Standardization. 2020.
738:
of their target architectures. The documentation for
515:
Non-equivalence of arithmetic right shift and division
575:
The (1999) ISO standard for the programming language
486:
with the arithmetic shift, especially in the case of
1041:. Reading, Mass.: Addison-Wesley. pp. 169โ170.
465:The formal definition of an arithmetic shift, from
1180:"ISO/IEC 14882:2020(E) โ Programming Language C++"
942:
1192:
431:for arithmetic and logical shifts respectively.
1134:International Organization for Standardization
1098:VAX MACRO and Instruction Set Reference Manual
821:
1039:, Volume 2 — Seminumerical algorithms
571:Handling the issue in programming languages
1128:
960:
1071:
1178:
1144:The Art of ASSEMBLY LANGUAGE PROGRAMMING
1048:"Arithmetic shifting considered harmful"
984:
906:"Arithmetic Shifting Considered Harmful"
269:
240:
228:
218:
26:
18:
1193:
1045:
948:
16:Shift operator in computer programming
1030:
903:
815:"Annotated Ada 2012 Reference Manual"
706:taking unsigned arguments. These are
659:
1140:
936:
890:Thomas R. Cain and Alan T. Sherman.
460:
1162:
972:
13:
1092:
1058:(11). New York: ACM Press: 61โ69.
963:, ยง 6.5.7 Bitwise shift operators.
827:
753:
14:
1217:
1094:"3.7.1 Arithmetic Shift Operator"
1046:Steele, Guy L. (November 1977).
1004: This article incorporates
999:
975:, ยง 4.5 Integers implementation.
552:make such incorrect statements.
511:whereas logical shifts do not .
1130:Programming languages — C
1037:The Art of Computer Programming
1017:General Services Administration
993:
918:from the original on 2022-10-09
897:
892:"How to break Gifford's cipher"
884:
873:from the original on 2022-10-09
847:
833:
767:"Bit manipulation - Dlang Tour"
731:
713:
680:
586:
866:. 2019-12-13. pp. 18โ20.
807:
783:
759:
645:
632:
623:
605:
423:Some authors prefer the terms
1:
748:
698:taking a signed argument and
7:
1052:ACM SIGPLAN Notices Archive
477:numeration system and in a
10:
1222:
987:, ยง 7.6.7 Shift operators.
528:convention for division.
1172:Free Software Foundation
599:
1206:Operators (programming)
740:GNU Compiler Collection
139:Shift_Right_Arithmetic
1088:on September 22, 2017.
1012:Federal Standard 1037C
1006:public domain material
841:"Z80 Assembler Syntax"
467:Federal Standard 1037C
397:arithmetic right shift
36:
35:is filled with a zero.
24:
1132:. ISO/IEC 9899:1999.
1064:10.1145/956641.956647
690:class from Haskell's
429:zero-fill right-shift
393:arithmetic left shift
387:, sometimes termed a
45:Language or processor
33:least significant bit
30:
22:
694:module defines both
519:However, arithmetic
453:For example, in the
377:computer programming
108:(signed types only),
509:arithmetic overflow
455:x86 instruction set
113:(signed types only)
41:
1164:"C Implementation"
425:sticky right-shift
39:
37:
25:
1201:Binary arithmetic
795:doc.rust-lang.org
461:Formal definition
405:bitwise operation
373:
372:
1213:
1186:
1184:
1175:
1159:
1157:
1156:
1147:. Archived from
1137:
1125:
1119:
1115:
1113:
1105:
1089:
1084:. Archived from
1075:
1042:
1025:
1024:
1019:. Archived from
1003:
1002:
988:
982:
976:
970:
964:
958:
952:
946:
940:
939:, ยง 6.6.2.2 SAR.
934:
928:
927:
925:
923:
917:
910:
904:Steele Jr, Guy.
901:
895:
888:
882:
881:
879:
878:
872:
859:
851:
845:
844:
837:
831:
825:
819:
818:
811:
805:
804:
802:
801:
787:
781:
780:
778:
777:
763:
743:
735:
729:
717:
711:
705:
701:
697:
693:
689:
684:
678:
676:
672:
668:
667:arithmetic-shift
663:
657:
649:
643:
636:
630:
627:
621:
614:
609:
565:ones' complement
558:two's complement
525:two's complement
444:two's complement
381:arithmetic shift
369:
365:
360:
356:
343:
338:
325:
320:
307:
302:
289:
284:
272:
260:
255:
243:
231:
230:arithmetic-shift
208:
203:
191:
186:
174:
169:
157:
152:
140:
135:
123:
118:
42:
38:
1221:
1220:
1216:
1215:
1214:
1212:
1211:
1210:
1191:
1190:
1189:
1182:
1154:
1152:
1117:
1116:
1107:
1106:
1009:
1000:
998:
996:
991:
983:
979:
971:
967:
961:ISOIEC9899 1999
959:
955:
947:
943:
935:
931:
921:
919:
915:
908:
902:
898:
889:
885:
876:
874:
870:
857:
853:
852:
848:
839:
838:
834:
826:
822:
813:
812:
808:
799:
797:
789:
788:
784:
775:
773:
765:
764:
760:
756:
754:Cross-reference
751:
746:
736:
732:
718:
714:
703:
699:
695:
691:
687:
685:
681:
674:
670:
666:
664:
660:
650:
646:
637:
633:
628:
624:
612:
610:
606:
602:
589:
573:
517:
500:
490:representation.
469:is that it is:
463:
367:
363:
358:
354:
341:
336:
323:
318:
305:
300:
287:
282:
271:Data.Bits.shift
270:
258:
253:
241:
229:
216:macro language
206:
201:
189:
184:
172:
167:
155:
150:
138:
133:
121:
116:
109:
104:
17:
12:
11:
5:
1219:
1209:
1208:
1203:
1188:
1187:
1176:
1160:
1138:
1126:
1104:on 2011-08-08.
1090:
1043:
1027:
1023:on 2022-01-22.
995:
992:
990:
989:
977:
965:
953:
941:
929:
911:. MIT AI Lab.
896:
883:
846:
832:
820:
806:
782:
771:tour.dlang.org
757:
755:
752:
750:
747:
745:
744:
730:
712:
679:
658:
644:
631:
622:
603:
601:
598:
588:
585:
572:
569:
516:
513:
499:
496:
492:
491:
488:floating-point
462:
459:
418:sign extension
401:binary numbers
385:shift operator
371:
370:
361:
352:
345:
344:
339:
334:
327:
326:
321:
316:
309:
308:
303:
298:
291:
290:
285:
280:
274:
273:
268:
262:
261:
256:
251:
245:
244:
239:
233:
232:
227:
221:
220:
217:
210:
209:
204:
199:
193:
192:
187:
182:
176:
175:
170:
165:
159:
158:
153:
148:
142:
141:
136:
131:
125:
124:
119:
114:
53:
52:
49:
46:
15:
9:
6:
4:
3:
2:
1218:
1207:
1204:
1202:
1199:
1198:
1196:
1181:
1177:
1173:
1169:
1165:
1161:
1151:on 2007-11-23
1150:
1146:
1145:
1139:
1135:
1131:
1127:
1123:
1111:
1103:
1099:
1095:
1091:
1087:
1083:
1079:
1074:
1069:
1065:
1061:
1057:
1053:
1049:
1044:
1040:
1038:
1033:
1032:Knuth, Donald
1029:
1028:
1026:
1022:
1018:
1014:
1013:
1007:
986:
985:ISOCPP20 2020
981:
974:
969:
962:
957:
950:
945:
938:
933:
914:
907:
900:
893:
887:
869:
865:
864:
856:
850:
842:
836:
829:
824:
816:
810:
796:
792:
786:
772:
768:
762:
758:
741:
734:
727:
723:
716:
709:
683:
662:
654:
648:
641:
635:
629:Fortran 2008.
626:
619:
608:
604:
597:
594:
584:
581:
578:
568:
566:
561:
559:
553:
551:
547:
543:
539:
534:
529:
526:
522:
512:
510:
505:
495:
489:
485:
484:logical shift
480:
476:
472:
471:
470:
468:
458:
456:
451:
448:
445:
441:
437:
432:
430:
426:
421:
419:
415:
411:
410:logical shift
406:
402:
398:
394:
390:
386:
382:
378:
362:
353:
351:
347:
346:
340:
335:
333:
329:
328:
322:
317:
315:
311:
310:
304:
299:
297:
293:
292:
286:
281:
279:
276:
275:
267:
264:
263:
257:
252:
250:
247:
246:
238:
235:
234:
226:
223:
222:
215:
212:
211:
207:>>>
205:
202:<<<
200:
198:
195:
194:
188:
183:
181:
178:
177:
171:
166:
164:
161:
160:
154:
149:
147:
144:
143:
137:
132:
130:
127:
126:
120:
115:
112:
107:
102:
98:
94:
90:
86:
82:
78:
74:
70:
66:
62:
58:
55:
54:
50:
47:
44:
43:
34:
29:
21:
1167:
1153:. Retrieved
1149:the original
1143:
1129:
1102:the original
1097:
1086:the original
1055:
1051:
1035:
1021:the original
1011:
997:
994:Sources used
980:
968:
956:
944:
932:
920:. Retrieved
899:
886:
875:. Retrieved
861:
849:
835:
823:
809:
798:. Retrieved
794:
785:
774:. Retrieved
770:
761:
733:
725:
721:
715:
682:
661:
647:
642:right shift.
639:
634:
625:
617:
607:
590:
587:Applications
582:
574:
562:
554:
546:Data General
530:
520:
518:
503:
501:
493:
464:
452:
446:
439:
435:
433:
428:
424:
422:
396:
392:
389:signed shift
388:
380:
374:
57:ActionScript
1118:|work=
1073:1721.1/6090
949:Steele 1977
593:downscaling
502:Arithmetic
479:fixed-point
237:Common Lisp
180:Standard ML
134:Shift_Left
1195:Categories
1168:GCC manual
1155:2007-11-28
877:2021-08-07
800:2022-11-13
776:2019-06-23
749:References
708:isomorphic
665:In Scheme
442:bits on a
348:Assembly:
330:Assembly:
312:Assembly:
294:Assembly:
190:~>>
65:JavaScript
1120:ignored (
1110:cite book
937:Hyde 1996
692:Data.Bits
677:variants.
613:>>
185:<<
122:>>
117:<<
1082:15420308
1034:(1969).
973:FSF 2008
913:Archived
868:Archived
722:unsigned
533:division
414:sign bit
403:it is a
395:and the
1174:. 2008.
1136:. 1999.
828:HP 2001
653:OpenVMS
651:In the
640:logical
618:logical
266:Haskell
214:OpenVMS
197:Verilog
173:SHIFTA
168:SHIFTL
163:Fortran
1080:
922:20 May
863:GitHub
726:signed
704:shiftR
700:shiftL
671:-right
620:shift.
548:, and
447:signed
399:. For
350:RISC-V
225:Scheme
146:Kotlin
69:Python
51:Right
1183:(PDF)
1078:S2CID
1008:from
916:(PDF)
909:(PDF)
871:(PDF)
858:(PDF)
696:shift
675:-left
600:Notes
521:right
475:radix
383:is a
379:, an
249:OCaml
111:Swift
101:Julia
1122:help
924:2013
724:and
688:Bits
686:The
673:and
611:The
550:ANSI
504:left
427:and
368:srai
359:slli
278:VHDL
156:shr
151:shl
106:Rust
77:Ruby
61:Java
48:Left
1068:hdl
1060:doi
542:IBM
538:DEC
420:).
375:In
364:sra
355:sll
342:ASR
337:ASL
332:68k
324:SAR
319:SAL
314:x86
306:SRA
301:SLA
296:Z80
288:sra
283:sla
259:asr
254:lsl
242:ash
129:Ada
85:C++
73:PHP
59:3,
1197::
1170:.
1166:.
1114::
1112:}}
1108:{{
1096:.
1076:.
1066:.
1056:12
1054:.
1050:.
1015:.
860:.
793:.
769:.
544:,
540:,
366:,
357:,
219:@
99:,
97:Go
95:,
93:C#
91:,
83:,
79:,
75:,
71:,
67:,
63:,
1158:.
1124:)
1070::
1062::
951:.
926:.
880:.
843:.
830:.
817:.
803:.
779:.
702:/
577:C
440:n
436:n
103:,
89:D
87:,
81:C
Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.