3
int x = 2;

x = rotateInt('L', x, 1); // should return 4

x = rotateInt('R', x, 3); // should return 64

Here is the code, can someone check it and let me know what the error is?

Compilation is successful, but it says Segmentation Fault when I execute it.

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;   
 }
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Tim
  • 87
  • 1
  • 1
  • 8
  • 3
    If you're done with your related question from 10 min ago http://stackoverflow.com/questions/3928659/rotating-bits-of-any-integer-in-c you might want to accept an answer before moving on. – Dusty Oct 14 '10 at 00:20

4 Answers4

9

Start honing your debugging skills now. If you are going to be any form of an engineer, you'll need to write programs of some variety, and will thus be debugging all of your life.

A simple way to start debugging is to put print statements in to see how far your code makes it before it dies. I recommend you start by isolating the error.

San Jacinto
  • 8,774
  • 5
  • 43
  • 58
  • it prints nothing but segmentation fault, that is it. – Tim Oct 14 '10 at 00:40
  • That mens it's crashing before your first `print` statement. Move the print statement earlier and retry. Repeat until you know which line is causing the segfault – Josh Oct 14 '10 at 00:49
  • 1
    @Josh that's exactly what I was going to say, thanks. Don't forget to put a \n in your printf statement to flush the stream. – San Jacinto Oct 14 '10 at 00:51
  • For bonus points, learn how to use a debugger. You'll usually find the problem much faster than with printfs. – bstpierre Oct 14 '10 at 01:18
  • @bstpierre true, but I figured that if we hadn't discovered printf-debugging yet that we weren't quite ready for the concept of a debugger :) – San Jacinto Oct 14 '10 at 01:38
2

Not sure about the seg fault, but I think

if((x & 128) == 1)  

should be

if((x & 128) == 128)

or just

if(x & 128)  
Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
1

I tried on my computer (MacBookPro / Core2Duo) and it worked. By the way, what's your target architecture ? Some (many) processors perform rotation instead of shifts when you use the C operators ">>" and "<<".

Antoine Trouve
  • 1,198
  • 10
  • 21
  • OK, so I asmume it is an Intel Machine, which does not rotate (at least it is not the case of ny Intel Core2Duo). However it masks the argument with 0xff in the case of integer rotation (i.e. "0xff00 >> 16 = 0" but "0xff00 >> 40 = 0xff"). – Antoine Trouve Oct 14 '10 at 00:58
  • A compiler that did an actual bit rotation rather than a pure shift for the `<<` and `>>` operators would be non-compliant. I don't see any wiggle-room in the definitions of the operators. They are shifts and not rotates, and have been since the dawn of the language. See C99, section 6.5.7 where it says "The result of `E1 << E2` is `E1` left-shifted `E2` bit positions; vacated bits are filled with zeros." It goes on to describe the boundaries of the defined behavior in gory detail. Right shifts are described similarly, but with slightly different boundaries. – RBerteig Oct 14 '10 at 00:59
  • It is not a matter of compiler, but processor. For instance PowerPC's shift instruction does rotation. – Antoine Trouve Oct 14 '10 at 01:01
  • @Antoine, shifting by more bits than the number of bits in your integral type is explicitly Undefined Behavior. Anything can happen. The compiler is not obligated to be "reasonable" or to generate code that does something "reasonable". – RBerteig Oct 14 '10 at 01:02
  • 1
    @Antoine, rotation is simply not allowed as the implementation of the << operator. The language of the standard is *extremely* clear. It goes on to say that `E1< – RBerteig Oct 14 '10 at 01:05
  • OK, but "0xff00 << 24" with gcc on my PowerPC retrieves "0xff". So I guess you can say that gcc does not fit the standard, but we are working with compilers, not standards. Anyway, for my personal enhancement (sorry for Josh), do you have a link to the part of the reference which says that ? – Antoine Trouve Oct 14 '10 at 01:09
  • @Antoine gcc on Suse x86_64 produces 0 for me with that operation. I HIGHLY doubt that gcc made an exception to break the standard just for the PowerPC architecture. – San Jacinto Oct 14 '10 at 11:06
0

When you use ^ don't you mean the or operator | ?

No one in particular
  • 2,682
  • 2
  • 17
  • 20