Thread: Right Shift Operator

  1. #16
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,908
    I think that we are getting a bit off track, but it is also worth noting that although two's compliment is the most popular representation of a negative number, the C standards do not specify that it has to be that.

    That is why...
    Quote Originally Posted by [URL="https://cboard.cprogramming.com/member.php?u=86607"
    flp1969[/URL]]Right shifts are 'undefined behavior' if done with signed integers
    Quote Originally Posted by C99 6.5.7 Bitwise shift operators
    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.

    *Note that the ^ was added by my to indicate superscript - i.e. "2 to the power of E2"


    Code:
    // This could equal anything depending on the environment that it is running on...
    banana = -7 << 1;
    Fact - Beethoven wrote his first symphony in C

  2. #17
    Registered User
    Join Date
    Feb 2019
    Posts
    881
    Quote Originally Posted by Click_here View Post
    I think that we are getting a bit off track, but it is also worth noting that although two's compliment is the most popular representation of a negative number, the C standards do not specify that it has to be that.
    Previously, I said: "And, beware: Right shifts are 'undefined behavior' if done with signed integers."

    I was only trying to explain what two's complement is...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. shift operator
    By Satya in forum C Programming
    Replies: 3
    Last Post: 01-05-2016, 11:56 AM
  2. Shift operator doubt
    By thannara123 in forum C Programming
    Replies: 3
    Last Post: 06-19-2015, 08:14 AM
  3. Bitwise Operator Shift
    By YannB in forum C Programming
    Replies: 14
    Last Post: 03-04-2014, 09:49 PM
  4. unsigned right shift operator
    By abraham2119 in forum C Programming
    Replies: 2
    Last Post: 06-05-2009, 11:01 AM
  5. left shift operator!!!!
    By theblackhat in forum C Programming
    Replies: 2
    Last Post: 10-02-2004, 02:07 AM

Tags for this Thread