#### Howdy, Stranger!

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

#### Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmers 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 it's exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# Rotate an integer

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..
· ·

• 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

· ·
• 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.
· ·
• 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

· ·
• 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;
}

· ·
• 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

· ·
• 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]
· ·
• 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

· ·
• 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]
· ·