Question: I am trying to clearly understand the behaviour of the shift operators (especially for the boundary cases), so I devised a little test written in C++.

```
    int a = odd_value; //321 in my case but it should not matter (the last bit to be 1)
    print(0, a);

    a = a << 31; // (this gives a segmentation fault, as normal because it tries the sign bit becomes 1 but all the other bits are 0).
    print(0, a); //the segmentation fault happens here - it prints the minimum integer value and then gives the fault
    a = (a << 1) + 1; // but if then I do this, shouldn't it set a to -1 ??
    print(a); //gives 0

   void print(int stackCallIndex, int nb)
   {
     if(nb)
     {
        print(++stackCallIndex, nb >> 1);
        if(nb & 1) printf("%d", 1);
        else       printf("%d", 0);

        if(stackCallIndex % 8 == 0) printf(" ");
     }
   }

```

Answer: 
According to your code I'd bet on a stackoverflow due to infinite recursion if you try to print negative values.

```
   void print(int stackCallIndex, int nb)
   {
     if(number)
     {
        print(++stackCallIndex, nb >> 1); // likely an infinite recursion here.
        if(nb & 1) printf("%d", 1);
        else       printf("%d", 0);

        if(stackCallIndex % 8 == 0) printf(" ");
     }
   }

```

so why would it be infinite recursion? Strictly according to standard right shifting negative signed integers is implementation defined.

In most implementations it will do an [arithmetic right shift](https://en.wikipedia.org/wiki/Arithmetic_shift), meaning that `1111 1100` (-4 in two's complement 8bit) shifted to the right by 1 would result in `1111 1110` (-2 in two's complement 8bit) as you can see you always fill up the sign bit again so your number would never get to 0 and the if condition is always true.

---

Generally doing bit manipulation on signed values is a bad idea, they involve implementation/undefined behavior in a few cases. Better cast all values to unsigned before using bit manipulation.
When I try that I get -214783648 which is the smallest known value in integer... this means that you are making an integer bigger than the allowed range that's why in your case you get a segmentation fault...