### Shift operators: << and >>

• The left shift shift operator of C

• Left shift

 x << n = left shift the bit pattern x over n positions The right-most bit positions will be filled with 0 bits

Example:

 ``` char a = 0b00000001; char b = a << 1; /* b = 00000010 */ char c = a << 2; /* c = 00000100 */ ```

• The right shift shift operator of C

• Right shift

• x >> n = right shift the bit pattern x over n positions

• Filler bit used:

• If the type of the variable x is an unsigned type:

 The left-most bit positions will always be filled with 0 bits

• If the type of the variable x is an signed type:

 The left-most bit positions will be filled with 0 bits if the left-most bit of x is equal to 0 The left-most bit positions will be filled with 1 bits if the left-most bit of x is equal to 1

Example 1: unsigned type

 ``` unsigned char a = 0b00000100; unsigned char b = 0b10000100; char c; c = a >> 1; /* c = 00000010 */ c = b >> 1; /* c = 01000010 */ ```

Example 2: signed type

 ``` char a = 0b00000100; char b = 0b10000100; char c; c = a >> 1; /* c = 00000010 */ c = b >> 1; /* c = 11000010 */ ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   gcc shift1.c To run:          ./a.out

Delete the unsigned keyword, re-compile it and re-run it !!!

• Using the shift operators

• Common usage of the shift operators

• Shift left one bit = multiply a value by 2

Example:

 ``` Bit pattern | Value in decimal Left shift 1 bit | Value in decimal --------------+-------------------- -------------------+------------------- 00000011 | 3 00000110 | 6 00000101 | 5 00001010 | 10 ```

• Shift right one bit = divide a value by 2 (= quotient of a division by 2)

Example:

 ``` Bit pattern | Value in decimal Right shift 1 bit | Value in decimal --------------+-------------------- --------------------+------------------ 00000110 | 6 00000011 | 3 (= 6%2) 00000011 | 3 00000001 | 1 (= 3%2) 00000101 | 5 00000010 | 2 (= 5%2) ```

• Example C program:

 ``` int main( int argc, char* argv[] ) { char a = 1; /* a = 00000001 (= 1) */ a = a << 1; /* Now: a = 00000010 (= 2) */ printf( "a = %d\n", a ); a = a << 1; /* Now: a = 00000100 (= 4) */ printf( "a = %d\n", a ); a = a >> 1; /* Now: a = 00000010 (= 2) */ printf( "a = %d\n", a ); a = a >> 1; /* Now: a = 00000001 (= 1) */ printf( "a = %d\n", a ); a = a >> 1; /* Now: a = 00000001 (= 0) */ printf( "a = %d\n", a ); } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   gcc mult-div1.c To run:          ./a.out

• Example using shift operation: print an int variable in bit representation

• Recall that int (integer) typed variables use the binary number system to represent values

Examples:

 ``` What is stored in an int variable | Value that is represented --------------------------------------+------------------------------- 00000000 00000000 00000000 00000000 | 0 00000000 00000000 00000000 00000001 | 1 00000000 00000000 00000000 00000010 | 2 00000000 00000000 00000000 00000011 | 3 .... ... ```

In other words:

• When you write the following assignment statement:

 ``` int x; x = 3; ```

• the computer will store the following bit pattern in the variable x:

 ``` 00000000 00000000 00000000 00000011 ```

• Problem description:

 Write an algorithm to print the bit pattern stored inside an int variable a (in binary format)

Pseudo code:

 ``` bit position 31 bit position 0 | | v v a = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (32 bits) ^ | bit position i for ( i = 31; i >= 0; i-- ) if ( bit at position i in variable a == 1 ) print '1'; else print '0'; ```

• C program:

 ``` void printBits( int a ) { int i; /* 10987654321098765432109876543210 */ unsigned int mask = 0b10000000000000000000000000000000; for ( i = 31; i >= 0; i-- ) { if ( (a & mask) != 0 ) putchar('1'); else putchar('0'); mask = mask >> 1; /* Shift mask bit to right */ } } ```

Note:

 I have written the algorithm as a C function Yes, I know that we have not covered C functions yet, but the syntax of C functions is very similar to that of Java (but without the class construct around it) The putchar(x) function will print the character x to the standard output (terminal).

• Whole C program:

 ``` void printBits( int a ) { int i; /* 10987654321098765432109876543210 */ unsigned int mask = 0b10000000000000000000000000000000; for ( i = 31; i >= 0; i-- ) { if ( (a & mask) != 0 ) putchar('1'); else putchar('0'); mask = mask >> 1; /* Shift mask bit to right */ } } int main( int argc, char* argv[] ) { int a; printf( "a = " ); scanf( "%d", &a ); printf( "a = %d\n", a ); printf( "Binary representation = " ); printBits( a ); /* Call the printBits() function */ putchar('\n'); } ```

Remember that: a C program always starts running in the main() function (just like Java)

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   gcc pr-bits.c To run:          ./a.out

• Final note

• These shift operators are also defined in Java.

• They are often omitted in introduction courses because they are not used in program at the introduction level.