Rotierende Bits einer beliebigen Ganzzahl in C
Übergeben Sie dieser Funktion eine Ganzzahl 2 und geben Sie dann eine Ganzzahl von 4 @ zurüc
x = 2;
x = rotateInt('L', x, 1);
(links die Bits um 1 verschieben)
Beispiel: 00000010 -> um 1 nach links drehen -> 00000100
aber wenn ich das übergebe:
x = rotateInt('R', x, 3);
it gibt 64, 01000000 @ zurü
Hier ist der Code, kann jemand den Fehler beheben ... danke
int rotateInt(char direction, unsigned int x, int y)
{
unsigned int mask = 0;
int num = 0, result = 0;
int i;
for (i = 0; i < y; i++)
{
if (direction == 'R')
{
if ((x & 1) == 1)
x = (x ^ 129);
else
x = x >> 1;
}
else if (direction == 'L')
{
if ((x & 128) == 1)
x = (x ^ 129);
else
x = x << 1;
}
}
result = (result ^ x);
return result;
}