Results 1 to 4 of 4

Thread: rol & ror

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Location
    York
    Posts
    197

    rol & ror

    Is there a way of rotating bits in C?

    e.g. in asm,

    rol eax, 4
    ror eax, 3 etc.

    I know i could use inline asm, but i was wondering if there was a C equiv

    Cheers.

  2. #2
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: rol & ror

    i am not sure though, forgot the working of rol and ror, but maybe '<<' and '>>' operators could do this.
    Code:
    int num = 1;
    num = num << 1; //will give you 2
    num = num << 2; //will give you 8
    num = num >> 1; //will give you 4
    Last edited by Harsh Gupta; Jul 23rd, 2006 at 09:52 AM.
    Show Appreciation. Rate Posts.

  3. #3
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771

    Re: rol & ror

    There are no operators for rotation in C++, you will have to work with shifts (<< and >>). A left rotate by x can be written, assuming 32 bit integers, as (a << x | a >> 32-x).
    Maybe the compiler is smart enought to optimize this into a rotate instruction.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Location
    York
    Posts
    197

    Re: rol & ror

    Ok thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width