## C and C++

Moderators: None (Apply to moderate this forum)
Number of posts: 94711

This Forum Only

Rotate an integer Posted by progeni on 20 May 2006 at 9:32 PM
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..
Re: Rotate an integer Posted by bilderbikkel on 21 May 2006 at 2:24 AM
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

Re: Rotate an integer Posted by IDK on 21 May 2006 at 8:13 AM
: 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.
Re: Rotate an integer Posted by tsagld on 22 May 2006 at 1:28 AM
: : 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

Re: Rotate an integer Posted by FDrache on 22 May 2006 at 3:21 AM
: : : 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;
}

Re: Rotate an integer Posted by tsagld on 22 May 2006 at 3:25 AM
: : : : 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

Re: Rotate an integer Posted by AsmGuru62 on 22 May 2006 at 4:23 AM
```#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);
```

Nice trick, AsmGuru Posted by tsagld on 22 May 2006 at 5:10 AM
:
```: #define ROTATE_INTEGER_R(n) n=(((n) << 31) | ((n) >> 1))
: ```

Never saw it before.

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

Re: Rotate an integer Posted by IDK on 22 May 2006 at 6:38 AM
: : : 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:
```int i = 0;
int j = 0;
asm{
ror [i], [j]
}
```

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic