Search This Blog

Translate This Page

Wednesday, 27 February 2013

Rotate bits of a Numbers


Left and Right Rotation of BIT:     


#include <stdio.h>
#define BIT 32
int leftrotate(int n,int d)
{
        return (n<<d)|(n>>(BIT-d));
}
int rightrotate(int n,int d)
{
        return (n>>d)|(n<<(BIT-d));
}
int main()
{
        int n,d;
        printf ("Enter no\n");
        scanf ("%d",&n);
        printf ("how many bit left rotate\n");
        scanf ("%d",&d);
        printf (" left rotation of %d by %d is ",n,d);
        printf ("%d\n",leftrotate(n,d));
        printf (" right rotation of %d by %d is ",n,d);
        printf ("%d\n",rightrotate(n,d));
}

output:

Enter no
4
how many bit left rotate
2
 left rotation of 4 by 2 is 16
 right rotation of 4 by 2 is 1

1 comment:

Latest Current Visitor

World Clock Time, for View Click on Country