Rotate an integer - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Rotate an integer

progeniprogeni Posts: 20Member
Write a program to rotate an integer,m in the right hand direction ,by a specified no of bits,n. those who give the code pls explain as well.Thanks




Regs
progeni..

Comments

  • bilderbikkelbilderbikkel Posts: 754Member
    Use the function 'rotate'. If you don't have it, you'll have to write it or search Google. It is too complex to explain...
    bilderbikkel

  • IDKIDK Posts: 1,784Member
    : Write a program to rotate an integer,m in the right hand direction ,by a specified no of bits,n. those who give the code pls explain as well.Thanks
    :
    :
    :
    :
    : Regs
    : progeni..
    :
    Here's the program:
    ror ax, 1
    I think it's about 2 bytes or so.
  • tsagldtsagld Posts: 621Member
    : : Write a program to rotate an integer,m in the right hand direction ,by a specified no of bits,n. those who give the code pls explain as well.Thanks
    : :
    : :
    : :
    : :
    : : Regs
    : : progeni..
    : :
    : Here's the program:
    : ror ax, 1
    : I think it's about 2 bytes or so.
    :
    That's asm. Using C++, you will need a couple of hundred lines of code. Go to your teacher and tell him that this assignment is way too complex for a beginner. Please let us know his reaction...


    Greets,
    Eric Goldstein
    http://www.gvh-maatwerk.nl


  • FDracheFDrache Posts: 64Member
    : : : Write a program to rotate an integer,m in the right hand direction ,by a specified no of bits,n. those who give the code pls explain as well.Thanks
    : : Here's the program:
    : : ror ax, 1
    : : I think it's about 2 bytes or so.
    : :
    : That's asm. Using C++, you will need a couple of hundred lines of code.

    - Here are my 100 lines. If you need left rotation, too, think of using an extra bool in the call or negative numbers for by. Sorry no indentation possible. FDrache.

    int RotateRight(int Number, short by)
    {
    int i;
    bool on;

    by = By % 32; // for safety

    for (i = 1; i <= by; i++)
    {
    // The LSB goes lost by shifting right
    on = (Number & 1) > 0; // Save LSB
    Number = Number >> 1;
    if (on)
    Number |= 0x80000000; // Highest Bit of 32
    } // for i

    return Number;
    }


  • tsagldtsagld Posts: 621Member
    : : : : Write a program to rotate an integer,m in the right hand direction ,by a specified no of bits,n. those who give the code pls explain as well.Thanks
    : : : Here's the program:
    : : : ror ax, 1
    : : : I think it's about 2 bytes or so.
    : : :
    : : That's asm. Using C++, you will need a couple of hundred lines of code.
    :
    : - Here are my 100 lines. If you need left rotation, too, think of using an extra bool in the call or negative numbers for by. Sorry no indentation possible. FDrache.
    :
    : int RotateRight(int Number, short by)
    : {
    : int i;
    : bool on;
    :
    : by = By % 32; // for safety
    :
    : for (i = 1; i <= by; i++)
    : {
    : // The LSB goes lost by shifting right
    : on = (Number & 1) > 0; // Save LSB
    : Number = Number >> 1;
    : if (on)
    : Number |= 0x80000000; // Highest Bit of 32
    : } // for i
    :
    : return Number;
    : }
    :
    Come on! We were making fun of the initial poster. Didn't you get that?
    You are probably the first person here that did someone else's homework.



    Greets,
    Eric Goldstein
    http://www.gvh-maatwerk.nl


  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [code]
    #define ROTATE_INTEGER_R(n) n=(((n) << 31) | ((n) >> 1))

    int RotateIntValue (int n, int bits)
    {
    for (int i=0; i < bits; i++)
    {
    ROTATE_INTEGER_R (n);
    }
    }

    int a = 74843359;

    RotateIntValue (a, 7);
    [/code]
  • tsagldtsagld Posts: 621Member
    : [code]
    : #define ROTATE_INTEGER_R(n) n=(((n) << 31) | ((n) >> 1))
    : [/code]

    Never saw it before.



    Greets,
    Eric Goldstein
    http://www.gvh-maatwerk.nl


  • IDKIDK Posts: 1,784Member
    : : : Write a program to rotate an integer,m in the right hand direction ,by a specified no of bits,n. those who give the code pls explain as well.Thanks
    : : :
    : : :
    : : :
    : : :
    : : : Regs
    : : : progeni..
    : : :
    : : Here's the program:
    : : ror ax, 1
    : : I think it's about 2 bytes or so.
    : :
    : That's asm. Using C++, you will need a couple of hundred lines of code. Go to your teacher and tell him that this assignment is way too complex for a beginner. Please let us know his reaction...
    :
    :
    : Greets,
    : Eric Goldstein
    : http://www.gvh-maatwerk.nl
    :

    He didn't even say wich language to use...

    Since someone already have done the work, I can do it too...
    Here's three lines of code that will do it:
    [code]
    int i = 0;
    int j = 0;
    asm{
    ror [i], [j]
    }
    [/code]
Sign In or Register to comment.