6.11 – Bitwise operators – Shift operators

by subbu on September 30, 2013

Left shift operator <<

It is a bitwise binary operator used to shift the bits towards left to the specified number of places. It automatically fills leaved places at the right side with zeros.

Say for example, if 1 byte memory allocation (char) has 00001101 (13), left shift it by 3 using left shift operator results 01101000 (104)

left shift operator

#include <stdio.h>
int main()
{
char ch=13;
printf("%d",ch<<3);
return 0;
}

Output
104

Right shift operator >>

It is a bitwise binary operator used to shift the bits towards right to the specified number of places. It automatically fills leaved places at the left side with zeros and shifted digits towards right would be lost

Say for example, if 1 byte memory allocation (char) has 01101000 (104), right shift it by 4 using right shift operator results 0000110 (6)

Right shift operator

#include <stdio.h>
int main()
{
char ch=104;
printf("%d",ch>>4);
return 0;
}

Output
6

Note: Though we perform 1’s compliment, Left shift or Right shift operations on the data, the value stored in the variable is not changed unless the result is reassigned to the variable

#include <stdio.h>
int main()
{
char ch=13;
printf("~ch:%d",~ch);
printf("\nch<<3:%d",ch<<3);
printf("\nch>>3:%d",ch>>3);
printf("\nch:%d\n",ch);
return 0;
}

Output

~ch:-14
ch<<3:104
ch>>3:1
ch:13

In the above example though we have performed 1’s compliment, left shift and right shift operations on ch, the original value of ch (13) has not changed

Where do we use Shift Operators?

These are used to extract a series of required bits from the memory, later we use this technique to find the date of file created under file handling session.
Now we will see how different portions of binary number can be extracted using shift operators
If we want to know the date of a particular file created then a C function returns an unsigned integer from a 32 bits directory entry (will be covered later)
In the number first 7 bits gives year from 1980, next 4 bits gives the month and last 5 bits gives the day of month
Say for example, 9766 (0010 0110 0010 0110) is the extracted number from directory entry then 0010011 (11) is the year from 1980 that is 1999, 0001 (1) is the month and 00110 (6) is the day of month

extracting date

Now we will see how we can extract these parts using bitwise operators

short date=9766;
y=date>>9;

extracting date of month

We need to extract first 7 bits to get the year. Here the right shift bitwise operator (>>) shifts all the bits towards right for 9 places and nine 0’s are added at the left side. So the result would be the decimal equivalent to the first 7 bits of the date object, which would be the year from 1980.

x=date<<7;
m=x>>12;

extracting month of year

We need to extract four bits from 8th bit to 11th bit to get the month. Here the left shift operator (<<) shifts all the bits towards left for 7 places and 0’s are added at the right side.

Again the right shift operator (>>) shifts all the bits towards right for 12 places and 0’s are added at the left side. So the result would be the decimal equivalent from 8th bit to 11th bit, which would be the month.

x=date<<11;
d=x>>11;

extracting year

By using the same process, right most 5 bits are extracted by left shifting for 11 places and then right shifting for 11 places again, which would be the date.

#include <stdio.h>
int main()
{
short date=9766;
short d,m,y,x;
y=date>>9; /* from 1980 */
x=date<<7;
m=x>>12;
x=date<<11;
d=x>>11;
printf("%d-%d-%d",d,m,y+1980);
return 0;
}

Output
1-6-1999

Previous post:

Next post: