EmbLogic's Blog

how to rotate bits of unsigned integer to left/right by specific number of bits???

3 Responses to how to rotate bits of unsigned integer to left/right by specific number of bits???

  1. use array in this program as bits

  2. mandeeshc says:

    I guess Shift Left and Shift Right may help. <> ?

  3. msiddarth says:

    Go through this program and execute it you will get the result

    #include
    #define INT_BITS 32

    /* Driver program to test below functions */
    int main()
    {
    int n = 16;
    int d = 2;
    printf(“Left Rotation of %d by %d is “, n, d);
    printf(“%d”, leftRotate(n, d));
    printf(“\nRight Rotation of %d by %d is “, n, d);
    printf(“%d”, rightRotate(n, d));
    getchar();
    }

    /*Function to left rotate n by d bits*/
    int leftRotate(int n, unsigned int d)
    {
    /* In n<<d, last d bits are 0. To put first 3 bits of n at
    last, do bitwise or of n<>(INT_BITS – d) */
    return (n <> (INT_BITS – d));
    }

    /*Function to right rotate n by d bits*/
    int rightRotate(int n, unsigned int d)
    {
    /* In n>>d, first d bits are 0. To put last 3 bits of at
    first, do bitwise or of n>>d with n <> d)|(n << (INT_BITS – d));
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>