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

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    23

    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;
    
    
    	printf("Please enter an integer: ");
    	scanf_s("%d",&InputNumber);
    	
    	ShiftedNumber = InputNumber >> 2;
    
    
    	printf ("The number was shifted on bit level two places to the right: %d\n", ShiftedNumber);
    
    
    }
    Thanks in Advance!

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    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.
    Last edited by rcgldr; 05-26-2013 at 09:43 AM.

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    23
    could you pls explain this logic (((-1)>>1)<0) ? what does -1 represent?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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"?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2013
    Posts
    23
    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. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by Zach786 View Post
    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. #7
    Registered User
    Join Date
    May 2013
    Posts
    23
    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;
    
    
    	printf("Please enter an integer: ");
    	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");
    
    
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 05-19-2013, 04:16 PM
  2. logical shift and arithmetic shift.(bit shifting in C )
    By A34Chris in forum C Programming
    Replies: 20
    Last Post: 09-03-2012, 11:22 AM
  3. Logical Right Shift
    By theotherindian in forum C Programming
    Replies: 10
    Last Post: 09-22-2011, 01:26 PM
  4. logical not !7 logical whaaaa?
    By Charbot in forum C Programming
    Replies: 2
    Last Post: 03-22-2011, 07:19 PM
  5. arithmatic vs logical shift operation
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 02-21-2008, 04:21 AM