Shift-Right Operator >>

This page was translated by a robot.

The shift-right operator shifts the bits of an integer number to the right by a given number and pads the remaining bits with 0or with in the case of negative numbers 1. The number to be changed is to the left of the operator, the number of bit positions to be shifted is to the right of the operator.













01001001100101100000001011010010
00100100110010110000000101101001
00000010010011001011000000010110
10110110011010011111110100101110
11111101101100110100111111101001
#include <stdio.h>

void printbinary(int x){
  char str[33]; str[32] = '\0';
  int i = 32;
  while(i){i--; str[i] = (x & 1) + '0'; x>>=1;}
  printf("%s\n", str);
}

int main(){
  int x = 1234567890;
  int y = -1234567890;
  printbinary(x);
  printbinary(x >> 1);
  printbinary(x >> 5);
  printbinary(y);
  printbinary(y >> 5);
  return 0;
}

Also, in C++, the shift-right operator is often associated with input from a stream. See the comments on operator overloading below..

Details

The shift-right operator expects two operands as rvalues ​​and is processed from left to right . The return value is an rvalue , which is always an integer type.

The shift-right operator is only allowed for integer values. Under C++, the use of the operator on boolean values ​​is also permitted, but these are intconverted into one before the operator is used.

By shifting the bits to the right, the remaining positions at the left end are also 0filled up. This corresponds to a logical bit shift in assembler. However, if the type of the first operand is a signedtype, the sign bit (most significant bit) is copied. As a result, negative numbers remain negative, which corresponds to an arithmetic bit shift in assembler. The shift-right operator is often abbreviated to SHR.





01001001100101100000001011010010
00000010010011001011000000010110
11101110011010110010100000000000
00000111011100110101100101000000
01001001100101100000001011010010
00000010010011001011000000010110
10110110011010011111110100101110
11111101101100110100111111101001
unsigned  int x = 1234567890;
unsigned  int y = 4000000000;
signed    int z = 1234567890;
signed    int w = -1234567890;
printbinary(x);
printbinary(x >> 5);
printbinary(y);
printbinary(y >> 5);
printbinary(z);
printbinary(z >> 5);
printbinary(w);
printbinary(w >> 5);

If the number of bits to be shifted is negative or greater than the length of the integer type used in bits, the result is unspecified. Depending on the compiler, other results may arise. In GCC, for example, the number of bits to be unsignedshifted is taken as a number and calculated modulo the register width in bits, but the compiler issues a warning.

<>are mainly used for low-level programming, such as in systems programming. Since they change individual bits, dealing with these operators is also often referred to as bit fiddling (bit fiddling, also to be understood as bit tinkering, or in good Swiss German: Umegfätterle with Bittli). These two operators are rarely used in everyday programming.

An occasional use of the shift-right operator today is to divide a number by a power of 2. By shifting with 0, 1, 2, 3, ... bits, the first operand is divided with 1, 2, 4, 8, .... In earlier times, this was the preferred way of dividing numbers to a power of 2because a bit shift could be calculated by the processor faster than division. In today's processors, this no longer plays a role.

336
168
84
42
printf("%d\n", 336 >> 0);
printf("%d\n", 336 >> 1);
printf("%d\n", 336 >> 2);
printf("%d\n", 336 >> 3);