Computer Science

Shift Operator C

The Shift Operator C is a binary operator used in C programming language to shift the bits of a number to the left or right. The left shift operator (<<) shifts the bits to the left by the specified number of positions, while the right shift operator (>>) shifts the bits to the right. This operator is commonly used in low-level programming and bitwise operations.

Written by Perlego with AI-assistance

6 Key excerpts on "Shift Operator C"

  • C Programming
    eBook - ePub

    C Programming

    Learn to Code

    Figure 6.22 ) and check the result.
    Figure 6.22 Example program to test the left shift operator.
    Let us take another example for negative numbers (Figure 6.23 ). We know that negative numbers are represented in memory by the two’s complement method.
    Figure 6.23 Example program applying left shift to a negative number.
    To understand the output, we need to represent –13 using 16-bit representation:
    • +13 in 16-bit representation:
      0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1
    • One’s complement of the above representation:
      1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 0
    • Add 1 to the above result to get its two’s complement:
      1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1
    Finally, the two’s complement of –13 is 1111111111110011 which is actually represented in memory. We will apply a two-bit left shift to the above value to find the output (shown in Figure 6.24 ).
    Figure 6.24 Two-bit left-shift operation on –13.
    Similarly, for the right shift operator the bits are shifted to the right. Two programming examples and their output are shown in Figure 6.25 , which are self-explanatory.
    Figure 6.25 Example programs showing the output of the right shift operator.

    6.9 Special Operators

    Beside the operators described above, C supports some special operators that include the following. These operators have several uses, and we will discuss them with some examples that describe the characteristics of these operators.
    1. Comma operator;
    2. sizeof() operator;
    3. & and *;
    4. −> (arrow) and (Dot).
    In this section we will discuss the comma operator and the sizeof operator. Other operators will be discussed whenever there is a need for them.

    6.9.1 The Comma Operator

    The comma operator permits two different expressions to appear in situations where only one operation is ordinarily used. Comma separated operands can be chained together and evaluated in a left-to-right sequence with the right-most value yielding the result of the expression. Let us see this with an example (Figure 6.26
  • C++ Programming
    eBook - ePub
    • Yuan Dong, Fang Yang, Li Zheng(Authors)
    • 2019(Publication Date)
    • De Gruyter
      (Publisher)
    bit ^ 1 ) is the opposite of the original value.
    For example: if we want to flip the lowest four bits of 01111010, we can do the xor operation on it with 00001111:
    d)Bitwise complement(~)
    Bitwise complement is a uniary operator, and it is used to flip each bit of a binary number. For example:
    025 : 0000000000010101
    025 : 1111111111101010
    Fig. 2.2: The process of a>>2.
    Fig. 2.3: The process of 2<<1.
    e)Shift
    In C++ there are two shift operators: the left shift operator (<<) and right shift operator (>>). Both of them are binary operators. The left operand of a shift operator is the number you want to perform the shift on, and the right operand gives the number of bits you want to shift.
    Left shift shifts the binary value of the operand leftwards according to the designated number of bits. After the left shift, the vacated low bits will be filled with zero while the high bits that have been shifted out will be discarded.
    Right shift shifts the binary value of a number rightwards according to the designated number of bits. After the right shift, the operator will discard the low bits that have been shifted out. If the number is an unsigned number, the operator will fill the vacated high bits with zero; if the number is a signed number, then the operator will fill the vacated high bits with zero or the sign bit depending on different systems. In VC++6.0, it fills the vacated bits with the sign bit.
    Look at two examples:
    1. If the value of a char-variable a is −8, then its complement code is 11111000. The value of expression a >>2 is −2. Figure 2.2
  • Ivor Horton's Beginning Visual C++ 2012
    • Ivor Horton(Author)
    • 2012(Publication Date)
    • Wrox
      (Publisher)
    This shifts the value 16,387 two positions to the right, storing the value 4,096. Shifting right 2 bits is effectively dividing the value by 4 (discarding the remainder). This is also shown in the illustration.
    As long as bits are not lost, shifting n bits to the left is equivalent to multiplying the value by 2, n times. In other words, it is equivalent to multiplying by 2n . Similarly, shifting right n bits is equivalent to dividing by 2n . But beware: as you saw with the left shift of the variable number , if significant bits are lost, the result is nothing like what you would expect. However, this is no different from the multiply operation. If you multiplied the 2-byte number by 4, you would get the same result, so shifting left and multiply are still equivalent. The problem of accuracy arises because the value of the result of the multiplication is outside the range of a 2-byte integer.
    You might imagine that confusion could arise between the operators that you have been using for input and output and the shift operators. As far as the compiler is concerned, the meaning will always be clear from the context. If it isn’t, the compiler will generate a message, but you need to be careful. For example, if you want to output the result of shifting a variable number left by 2 bits, you could write the following statement:
    cout << (number << 2);
    Here, the parentheses are essential. Without them, the shift operator will be interpreted by the compiler as a stream operator, so you won’t get the result that you intended; the output would be the value of number followed by the value 2.
    The right-shift operation is similar to the left-shift. For example, suppose number has the value 24, and you execute the following statement:
    number >>= 2;
    This will result in number having the value 6, effectively dividing the original value by 4. However, the right shift operates in a special way with signed integer types that are negative, where the sign bit, which is the leftmost bit, is 1. In this case, the sign bit is propagated to the right. For example, declare and initialize a variable number of type char
  • Algebraic and Stochastic Coding Theory
    • Dave K. Kythe, Prem K. Kythe(Authors)
    • 2017(Publication Date)
    • CRC Press
      (Publisher)
    rotate through carry, instead of the logical or arithmetic shifts.
    The left and right shifts operations in the C family of languages are denoted by ‘≪’ and ‘≫’, respectively. The number of bits to be shifted is the same as above, i.e., x ‘≪’ b or x‘≫’. In Java, all integer types are signed, and the operators ≪ and ≫ perform arithmetic shifts; moreover, the operator ≫ performs the logical right shifts, but there is no operator ≪ because the arithmetic and logical left shift operations are identical in Java.
    2.2.12 One’s and Two’s Complement Arithmetic.
    One’s and two’s complement (or negative) of a binary number are part of a system of arithmetic that is devised to avoid the addition and subtraction circuitry, and examine the signs of the operands (plus or minus) to determine if the numbers are to be added or subtracted.
    The one’s complement of a binary number is performed by simply inverting every bit, i.e., by changing the 1s into 0s and 0s into 1s. The two’s complement of an N-bit binary number is obtained by subtracting the given number from a larger power 2
    N
    by computing its two’s complement. In practice, it is accomplished by first starting at the least significant bit (lsb), copying all zeros (working leftward toward the msb) until the first 1 is reached, then copying that 1, and reversing (flipping) all the remaining bits. The 2
    N
    -bit two’s complement system can be used for all binary numbers in the range −2
    N−1
    to +2
    N−1
    − 1.
    Example 2.2.2. The two’s complement for an 8-bit binary system is presented in Table 2.2.3
  • Ivor Horton's Beginning Visual C++ 2008
    • Ivor Horton(Author)
    • 2011(Publication Date)
    • Wrox
      (Publisher)
    This shifts the value 16,387 two positions to the right, storing the value 4,096. Shifting right two bits is effectively dividing the value by 4 (without remainder). This is also shown in the illustration.
    As long as bits are not lost, shifting n bits to the left is equivalent to multiplying the value by 2, n times. In other words, it is equivalent to multiplying by 2n . Similarly, shifting right n bits is equivalent to dividing by 2n . But beware: as you saw with the left shift of the variable number , if significant bits are lost, the result is nothing like what you would expect. However, this is no different from the multiply operation. If you multiplied the 2-byte number by 4 you would get the same result, so shifting left and multiply are still equivalent. The problem of accuracy arises because the value of the result of the multiplication is outside the range of a 2-byte integer.
    You might imagine that confusion could arise between the operators that you have been using for input and output and the shift operators. As far as the compiler is concerned, the meaning will always be clear from the context. If it isn't, the compiler will generate a message, but you need to be careful. For example, if you want to output the result of shifting a variable number left by 2 bits, you could write the following statement:
    cout << (number << 2);
    Here, the parentheses are essential. Without them, the shift operator will be interpreted by the compiler as a stream operator, so you won't get the result that you intended; the output will be the value of number followed by the value 2.
    In the main, the right shift operation is similar to the left shift. For example, suppose the variable number has the value 24, and you execute the following statement:
    number >>= 2;
    This will result in number having the value 6, effectively dividing the original value by 4. However, the right shift operates in a special way with signed integer types that are negative (that is, the sign bit, which is the leftmost bit, is 1). In this case, the sign bit is propagated to the right. For example, declare and initialize a variable number of type char
  • Learn C Programming from Scratch
    eBook - ePub

    Learn C Programming from Scratch

    A step-by-step methodology with problem solving approach (English Edition)

    x %= 5; (x = x % 5;)
  • <<= (Left shift and assign ): This operator shifts the bits of the value on the left side to the left by the number of positions specified by the value on the right side and assigns it to the left side variable. For example: x <<= 2; (x = x << 2;)
  • >>= (Right shift and assign ): This operator shifts the bits of the value on the left side to the right by the number of positions specified by the value on the right side and assigns it to the left side variable. For example: x >>= 2; (x = x >> 2;)
  • &= (Bitwise AND and assign ): This operator performs a bitwise AND operation between the value on the left side and the value on the right side and assigns it to the left side variable. For example: x &= 7; (x = x & 7;)
  • ^= (Bitwise XOR and assign ): This operator performs a bitwise XOR operation between the value on the left side and the value on the right side and assigns it to the left side variable. For example: x ^= 7; (x = x ^ 7;)
  • |= (Bitwise OR and assign ): This operator performs a bitwise OR operation between the value on the left side and the value on the right side and assigns it to the left side variable. For example: x |= 7; (x = x | 7;)
  • sizeof (): This operator returns the size of a variable. For example: float a;
    sizeof (a) , a being a float, shall return 4 (Bytes).
  • & (Addressof ) operator: Returns the address of a variable. For example:
    &a ; returns the actual address of the variable ‘a ’.
  • ? conditional operator: Conditional operator (ternary operator) is an alternate way of writing an if-else statement in C. Syntax: condition? expression1: expression2 For example: int max = a > b ? a : b;
    If expression a > b is true, a is returned as the expression’s value, else b is returned as the expression’s value.
  • Operator precedence in C
    Operator precedence controls how terms in an expression are grouped and how an expression is evaluated. Certain operators are more important than others; for example, the multiplication operator is more important than the addition operator.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.