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"

  • Book cover image for: Basic Computation and Programming with C
    Left shift (<<) operator is used to shift each bit in the operand to left and right shift (>>) operator is used to shift each bit in the operand to right. Shifting the operand n bit to the left is same as multiplying it by 2 n and shifting the operand n bit to the right is same as dividing (integer division) the number by 2 n . R EVIEW E XERCISES 1. What do you mean by bitwise operation? 2. Explain the operations of bitwise operators &, | and ^. 3. What are the left shift and right shift operators? Give some examples which implement those operators. 4. What is one’s complement operator? 5. What are the precedence and associativity of bitwise shift operators? 6. Using bitwise operator write a program in C to check whether an integer is odd or even. 7. Write a program to count number of 1s in the bit pattern of a given integer using bitwise operator. 8. Write a program to rotate right an unsigned integer n number of times. In each rotation each bit will be shifted one place and also recover the lost bit. 9. Write a program that sets one or more bits while other bits remain unchanged. 10. Write a program that inverts a set of bits while other bits remain unchanged. L EARN BY Q UIZ – Q UESTIONS 1 Which of the following is not a bitwise operator? (1) !! (2) ^ (3) && 2 What will be value of c? int a=5, b=6; int c=a|b; (1) 0 (2) 6 (3) 7 Bitwise Operators 523 3 What will be value of c? int a=5, b=6; int c=a&b; (1) 4 (2) 7 (3) 1 4 What will be value of c? int a=5, b=6; int c=a^b; (1) 3 (2) 6 (3) 0 5 What will be value of a and b after evaluating the expression? int a=5, b=6; a=a^b; b=a^b; a=a^b; (1) a=6, b=5 (2) a=6, b=6 (3) a=5, b=5 6 What will be the value of b? int a=4; b=a<<2; (1) 16 (2) 8 (3) 10 7. What will be the value of b? int a=4,b; b=a>>2; (1) 1 (2) 0 (3) 2 8 What will be the value of b? int a=0xfff0, b; b=~a; (1) 15 (2) 0 (3) 2 9. What will be the value of b? int a=−3, b; b=a>>2; Basic Computation and Programming with C 524 (1) −1 (2) 1 (3) 0 10.
  • Book cover image for: Programming Microcontrollers in C
    • Ted VanSickle(Author)
    • 2001(Publication Date)
    • Newnes
      (Publisher)
    Unlike most number systems, the binary system (or two’s complement system) allows the sign of the number to be contained in the binary representation of the number itself. These consider-ations lead to two different types of shifts for a system of binary numbers. A shift in which bits vacated by the shift are replaced by zeros is called a logical shift . All left shifts are logical shifts. As the shift progresses toward the left, bits that fill the number from the right will all be zero. Bits that shift out of the number on the left side are lost. A right shift can be either a logical or an arithmetic shift . If the type being shifted is signed, the sign bit—which is the leftmost bit—will propagate, retaining a number of the same sign. This is an arithmetic sign. If the number being shifted is unsigned, zeros are filled into the number from the left as the shift proceeds. In all cases, bits shifted out of a number by a shift operation will be lost. The one’s complement operator ~ is a unary operator that causes the bits in a variable to be reversed. Every 1 is replaced by a 0, and every 0 is replaced by a 1. The bitwise AND and OR operations are used to turn bits on and off. Suppose that we have a character variable r , and we wish to turn the least significant three bits off. Try r = r & ~7; In this case, the number 7 has each of the least significant bits turned on or 1. Therefore, the term ~7 has all of the bits in the number but the least significant turned on and these three bits are turned off or 0. Operators and Expressions 30 Chapter 1 Introduction to C When this mask is ANDed with r , all of the bits of r, with the exception of the least significant three bits, will be ANDed with a 1, and these bit values will remain unchanged. The least significant three bits will be AND ed with 0 and the result in these three bits will be 0. The bitwise OR will turn bits on. Suppose you wanted to turn bits 2 and 3 of r above on.
  • Book cover image for: 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
  • Book cover image for: Computer Programming with C++
    3.5.4 One’s complement operator (~) One’s complement of any binary number is obtained by bit inversion. Bit inversion is a process to replace every occurrence of 1 by 0 and every occurrence of 0 by 1 in a binary number. The operator ~ is a unary operator and hence it must be associated with exactly one operand. As an example, let us consider a variable x with a value 0 , as shown below: int x = 0; Now, let us apply a l’s complement operator (~) over x and store the l’s complement of variable x in the variable x itself. This can be done using the statement below: x = ~x; As the original value of the variable x is zero, the initial value of x can be represented in binary as shown below: x = 0000 0000 0000 0000 After applying 1’s complement operator on the variable x , the new value of the variable x will be as shown below: x = 1111 1111 1111 1111 Hence, the new value of x will be set as 65535, which is the decimal equivalent of the resultant binary number. 88 ✦ Computer Programming with C++ 3.5.5 Left shift operator (<<) The left shift operation is used to shift the bits of the binary number in the left direction by padding 0s from the right direction, as shown in Figure 3.13. Therefore, the left shift operator will delete the Most significant bits (MSBs) of the number because the extra 0s are padded as the least significant bits (LSBs) of the number. For example, let the value of the variable x be initialized to 4 using the statement below: int x = 4; The statement, l = x << 3; will shift the bits in number x by padding three extra 0s at the LSB side of x . The resultant number obtained after shifting the bits of x will be stored in the variable l, as shown in Figure 3.14 below.
  • Book cover image for: Microchip AVR Programming using ATmega Microcontrollers
    • Ivan Volosyak(Author)
    • 2021(Publication Date)
    • Shaker
      (Publisher)
    They are used to develop log- ical and mathematical expressions. There are many different types of operators, such as arithmetic (they were already used in task 5.1), bitwise, logical, relational and assignment operators in the C language. All of them are used very often, but bitwise operators are specifically suitable for bit operations in microcontroller programming. Table 5.3: Arithmetic C Operators Arithmetic Operators Description Example + Adds two operands x = a + b; - Subtracts two operands x = a - b; * Multiplies two operands x = a * b; / Divides two operands x = a / b; % Remainder of a division x = a % b; ++ Increment (increases the value by 1) a++; -- Decrement (decreases the value by 1) b--; The first 4 operators shown in Table 5.3 are addition, subtraction, multiplication, and division. The % operator returns the remainder of a division and is called the modulo operator (see also Q&A 11.9 on page 276). Common Mistake: Division of Integer Variables 1 int number1=250; 2 int number2=200; 3 int result = number1/number2; // Beginners expect 1.25, but the result is 1 When two integer numbers are divided, the result will be an integer. The ++ and -- operators can be used either as prefix (e.g. ++a;) or postfix operators (e.g. a++;). Prefix operators increase/decrease the value by 1 and then use the value of the corresponding variable (important is that this value is used in the further calculations in the same line of code). Postfix operators use the value of the variable first, then increase/decrease it by 1. #include #include #include "lcd.h" int main(){ char lcd _ line[25]; int number1=16; int number2=16; lcd _ init(); lcd _ clear(); sprintf(lcd _ line, "%d, %d, %d", number1++, number1, ++number2); // 16, 17, 17 lcd _ string(lcd _ line); return 0; } 82 5 Basic Microcontroller Programming Since the postfix operator is used in the variable number1, its original value is used (16) and converted into the string.
  • Book cover image for: Data Structures and Algorithms in C++
    • Michael T. Goodrich, Roberto Tamassia, David M. Mount(Authors)
    • 2011(Publication Date)
    • Wiley
      (Publisher)
    18 Chapter 1. A C++ Primer condition. For example, the following code first tests that a Passenger pointer p is non-null before accessing it. It would result in an error if the execution were not stopped if the first condition is not satisfied. if ((p != NULL) && p− >isFreqFlyer) . . . Bitwise Operators The following operators act on the representations of numbers as binary bit strings. They can be applied to any integer type, and the result is an integer type. ˜ exp bitwise complement exp & exp bitwise and exp ^ exp bitwise exclusive-or exp | exp bitwise or exp1 << exp2 shift exp1 left by exp2 bits exp1 >> exp2 shift exp1 right by exp2 bits The left shift operator always fills with zeros. How the right shift fills depends on a variable’s type. In C++ integer variables are “signed” quantities by default, but they may be declared as being “unsigned,” as in “unsigned int x.” If the left operand of a right shift is unsigned, the shift fills with zeros and otherwise the right shift fills with the number’s sign bit (0 for positive numbers and 1 for negative numbers). Note that the input (>>) and output (<<) operators are not in this group. They are discussed later. Assignment Operators In addition to the familiar assignment operator (=), C++ includes a special form for each of the arithmetic binary operators (+, −, *, /, %) and each of the bit- wise binary operators (&, |, ^, <<, >>), that combines a binary operation with assignment. For example, the statement “n += 2” means “n = n + 2.” Some examples are shown below. int i = 10; int j = 5; string s = "yes"; i −= 4; // i = i - 4 = 6 j *= −2; // j = j * (-2) = -10 s += " or no"; // s = s + “ or no” = “yes or no” These assignment operators not only provide notational convenience, but they can be more efficient to execute as well. For example, in the string concatenation example above, the new text can just be appended to s without the need to generate a temporary string to hold the intermediate result.
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.