# Thread: How to determine whether a logical or arithmetic shift is performed.

1. ## How to determine whether a logical or arithmetic shift is performed.

If input value was shifted to the right on bit level. How can I determine whether a logical or arithmetic shift is performed.

Code:
```#include <stdio.h>
#include <stdlib.h>

void main ()

{
printf("C- program that shifts any number two places to the right\n");

int InputNumber;
int ShiftedNumber;

scanf_s("%d",&InputNumber);

ShiftedNumber = InputNumber >> 2;

printf ("The number was shifted on bit level two places to the right: %d\n", ShiftedNumber);

}```

2. It should be stated in the compiler specifications. If not, assuming you're using a two's complement processor, then you could test: (((-1) >> 1) < 0). If the result is true, it's an arithmetic shift, if the result is false, it's a logical shift. If you think your processor might be one's or two's complement, you could use (((-3) >> 1) < 0) instead.

3. could you pls explain this logic (((-1)>>1)<0) ? what does -1 represent?

4. Originally Posted by Zach786
could you pls explain this logic (((-1)>>1)<0) ? what does -1 represent?
Well, from what you understand, what is the difference between a logical and an arithmetic shift? Also, do you understand what is meant by "one's complement" and "two's complement"?

5. Logical Shift: Shifting left by n bits on signed or unsigned binary number has effect of multiplying it by 2^n and Shifting right by n bits on an unsigned binary number has the effect of dividing by 2^n. Same concept applies for arithmetic shift expect when shift to right the empty position is filled with copy of the original number for example 00010111 shift to right by 1 give you 00001011. I don't fully understand one complement vs complement. I am reading up on right now.

6. Originally Posted by Zach786
could you pls explain this logic (((-1)>>1)<0) ? what does -1 represent?
-1 is a negative number. If a signed negative number is arithmetically shifted right, then the sign bit is copied into the upper shifted bits. On a two's complement processor, ((-1)>>1) == -1. For the other case, ((-3)>>1) == -2, and also ((-2)>>1) == -1.

A one's complement machine would be rare these days. The one's complement of a number just toggles all the bits. (For two's complement, all the bits are toggled, and the result is incremented). In ones complement, there are two zeros, positive zero and negative zero, but the processor has to treat both zeros as being zero:

ones complement example 16 bit values:
Code:
```0000 0000 0000 0011 == +3
0000 0000 0000 0010 == +2
0000 0000 0000 0001 == +1
0000 0000 0000 0000 == +0
1111 1111 1111 1111 == -0
1111 1111 1111 1110 == -1
1111 1111 1111 1101 == -2
1111 1111 1111 1100 == -3```
On a ones complement machine an arithmetic shift for ((-1)>>1) = -0 = +0 and for ((-3)>>1) == -1.

7. Arithmetic Right shift will preserve whether an integer is negative or positive. In a logical right shift, it will lose the sign indicator i.e. make negative number into a positive number. So I wrote if/esle statement which I think indicates what kind of shift was performed.

Code:
```#include <stdio.h>
#include <stdlib.h>

void main ()

{
printf("C- program that shifts any number two places to the right\n");

int InputNumber;
int ShiftedNumber;

scanf_s("%d",&InputNumber);

ShiftedNumber = InputNumber >> 2;

printf ("The number was shifted on bit level two places to the right: %d\n", ShiftedNumber);

if (InputNumber<0)
{
printf("Negative number was inserted\n");

if(ShiftedNumber<0)
{
printf("Arithmetic Right shift was performed\n");
}
else
{
printf("Logical Right shift was performed\n");
}

}
else
{
printf("Positive number was inserted\n");
printf("Arithmetic Right shift was performed\n");

}
}```