How can One Determine Whether a Right Shift of a Signed Value is Performed as an Arithmetic or a Logical Shift

Question: How can One Determine Whether a Right Shift of a Signed Value is Performed on Linux Machine as an Arithmetic or a Logical Shift

Answer: Right and Left shift operators are Bitwise Binary Operators operate on Individual Bits of their Integer Operands. On Linux Machine, Logical and Arithmetic Right and Left Shifts on Unsigned Integer values are same but Arithmetic Right Shift on signed integer values are implementation dependent.

/* rshift_negval.c -- What happens on right arithmetic shift on -ve int */
#include <stdio.h>
int main(void)
{
    int x = -1000, y, nbits;
 
    printf("By what no of bits u wanna right shift on signed integer, "
           "enter no of bits...\n");
    puts("Non integer value terminates the program.");
 
    while (scanf("%d", &nbits) == 1) {
        y = x >> nbits;
        printf("The value of x in y = x >> %d is %d and y is %d\n", 
               nbits, x, y);
    }
    printf("Program Terminated! Good Bye!\n");
    return 0;
}

When the above program is run, it displays the output as follows:

By what number of bits you want to right shift on signed integer, enter number of bits…
Non integer value terminates the program.
1
The value of x in y = x >> 1 is -1000 and y is -500
2
The value of x in y = x >> 2 is -1000 and y is -250
3
The value of x in y = x >> 3 is -1000 and y is -125
4
The value of x in y = x >> 4 is -1000 and y is -63
5
The value of x in y = x >> 5 is -1000 and y is -32
6
The value of x in y = x >> 6 is -1000 and y is -16
7
The value of x in y = x >> 7 is -1000 and y is -8
8
The value of x in y = x >> 8 is -1000 and y is -4
9
The value of x in y = x >> 9 is -1000 and y is -2
10
The value of x in y = x >> 10 is -1000 and y is -1
11
The value of x in y = x >> 11 is -1000 and y is -1
12
The value of x in y = x >> 12 is -1000 and y is -1
13
The value of x in y = x >> 13 is -1000 and y is -1
14
The value of x in y = x >> 14 is -1000 and y is -1
15
The value of x in y = x >> 15 is -1000 and y is -1
16
The value of x in y = x >> 16 is -1000 and y is -1
17
The value of x in y = x >> 17 is -1000 and y is -1
18
The value of x in y = x >> 18 is -1000 and y is -1
19
The value of x in y = x >> 19 is -1000 and y is -1
20
The value of x in y = x >> 20 is -1000 and y is -1
21
The value of x in y = x >> 21 is -1000 and y is -1
22
The value of x in y = x >> 22 is -1000 and y is -1
23
The value of x in y = x >> 23 is -1000 and y is -1
24
The value of x in y = x >> 24 is -1000 and y is -1
25
The value of x in y = x >> 25 is -1000 and y is -1
26
The value of x in y = x >> 26 is -1000 and y is -1
27
The value of x in y = x >> 27 is -1000 and y is -1
28
The value of x in y = x >> 28 is -1000 and y is -1
29
The value of x in y = x >> 29 is -1000 and y is -1
30
The value of x in y = x >> 30 is -1000 and y is -1
31
The value of x in y = x >> 31 is -1000 and y is -1
32
The value of x in y = x >> 32 is -1000 and y is -1000
q
Program Terminated! Good Bye!

Therefore, when right shift on a signed value (here -1000) is performed, on Linux Machine where type int has 32 bits storage, starting from 1 bit and successively increasing by 1, till 31 bits, the result is always negative. During the successive right shifts, signed integer number reduced to half of its before until becomes -1. And thereon it doesn’t alter with successive right shifts. When shifted the signed number by 32 bits, its wrap around.

advertisement
advertisement

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.