int x = 2;
x = rotateInt('L', x, 1); // should return 4
x = rotateInt('R', x, 3); // should return 64
Voici le code, quelqu'un peut-il le vérifier et laissez-moi savoir ce qu'est l'erreur?bit shift en C
La compilation est réussie, mais elle indique Segmentation Fault
lorsque je l'exécute.
int rotateInt(char direction, unsigned int x, int y)
{
int i;
for(i = 0; i < y; i++)
{
if(direction == 'R')
{
if((x & 1) == 1)
{
x = x >> 1;
x = (x^128);
}
else
x = x >> 1;
}
else if(direction == 'L')
{
if((x & 128) == 1)
{
x = x << 1;
x = (x^1);
}
else
x = x << 1;
}
}
return x;
}
Si vous avez terminé avec votre question connexe d'il ya 10 minutes http://stackoverflow.com/questions/3928659/rotating-bits-of-any-integer-in-c vous voudrez peut-être accepter une réponse avant de déménager sur. – Dusty