Thread: shift operation on a negative number.

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    10

    shift operation on a negative number.

    please explain shift operation on negative numbers.
    i executed a demo as below:
    Code:
    char c=-1,d,e;
    d=c<<9;
    e=c>>9;
    it yields d=0 ,which i could understand as it works like a normal shift operation where bits are discarded as they move out of the word and bits on right side get filled by all 0's,

    and e=-1,
    and this i could not figure out as even after shifting it by 9 bits it holds the value intact.


    any relevant explanation or reference would be a great help.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by avee137
    it yields d=0 ,which i could understand as it works like a normal shift operation where bits are discarded as they move out of the word and bits on right side get filled by all 0's,
    Strictly speaking, the behaviour is undefined. Using ** to denote exponentation:
    Quote Originally Posted by C99 Section 6.5.7 Paragraph 4
    The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros. If E1 has an unsigned type, the value of the result is E1 * 2**E2, reduced modulo one more than the maximum value representable in the result type. If E1 has a signed type and nonnegative value, and E1 * 2**E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.
    Quote Originally Posted by avee137
    and e=-1,
    and this i could not figure out as even after shifting it by 9 bits it holds the value intact.
    The result is implementation defined.
    Quote Originally Posted by C99 Section 6.5.7 Paragraph 5
    The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a nonnegative value, the value of the result is the integral part of the quotient of E1 / 2**E2. If E1 has a signed type and a negative value, the resulting value is implementation-defined.
    Generally, I prefer to work with unsigned integer types when dealing with bitwise operations.
    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

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    10
    Thank you.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Doesn't the compiler use logical shift for unsigned ints and arithmetic shift for signed ones?
    Devoted my life to programming...

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That would be a reasonable way to do it, but not all compilers are required to do so.

    It would certainly explain the phenomena that (-1 >> 9) is still -1. An arithmetic right shift shifts in a copy of the most significant (leftmost) bit. So if two's complement representation is used then -1 will be represented with all bits set to 1, e.g. 0xffffffff (on a 32-bit machine). Performing an arithmetic right-shift will shift out some 1's on the right, but the new bits shifted in will be 1's as well because the original MSB was 1. If a logical right-shift were performed then zeros are shifted in no matter what; so a right-shift by four bits would in this case be 0x0fffffff.

    The reasoning behind arithmetic left-shifts is quite simple: it preserves the negativeness of numbers. -4 right-shifted by 1 will be -2, which when right-shifted by 1 will be -1. Shifting in zeros in this case would have strange results if you were interpreting the bits as a signed number.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-04-2008, 08:15 PM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  4. arithmatic vs logical shift operation
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 02-21-2008, 04:21 AM
  5. How can i convert negative number to positive number ?
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 05-05-2004, 08:02 AM