bitwise shift operators in -ve numbers

This is a discussion on bitwise shift operators in -ve numbers within the C Programming forums, part of the General Programming Boards category; hi, Can anyone explain.....how the compiler computes the output for this ???? insert Code: int main() { printf("%x\n",-1>>4); } according ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    17

    bitwise shift operators in -ve numbers

    hi,
    Can anyone explain.....how the compiler computes the output for this ????
    insert
    Code:
    int main()
    {
       printf("%x\n",-1>>4);
    }
    according to me....-1 is represented as

    1000 0000 0000 0000 0000 0000 0000 0001
    High order bits lower order bits

    I am using microsoft VC++ compiler so int occupies 4 bytes.

    so when u right shift by 4 it should display 0.

    But it displays ffff....

    Can anyone explain how.....????

    Regards,
    Rohit

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    according to me....-1 is represented as

    1000 0000 0000 0000 0000 0000 0000 0001
    High order bits lower order bits
    Your assumption is false. In C, either a one's or two's complement representation is used (not sign and magnitude, if I remember correctly), so the 32 bit representation of -1 in binary would either be:
    1111 1111 1111 1111 1111 1111 1111 1110
    or:
    1111 1111 1111 1111 1111 1111 1111 1111

    EDIT:
    Oh, I did not remember correctly: sign and magnitude representation is allowed, but your implementation most likely uses two's complement anyway.
    Last edited by laserlight; 09-15-2008 at 05:35 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You have the negative numbers back to front. A negative number is indeed beginning with 1, but the "formula" for createing a negative number is "invert all bits, then add one" [1], which makes -1 the same as "all ones".

    [1] This is ONE of several techniques, called "two's complement", which is by far the most common method. It makes addition and subtraction of negative numbers fairly easy.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    Your assumption is false. In C, either a one's or two's complement representation is used <snip}
    Is that actually defined in the C standard, or just "by convention C uses ...". I personally haven't ever used a machine that uses one's complement, never mind any other variation of negative numbers.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Is that actually defined in the C standard, or just "by convention C uses ...". I personally haven't ever used a machine that uses one's complement, never mind any other variation of negative numbers.
    It is defined, or rather, the options for representing signed integers are enumerated.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Aug 2008
    Posts
    17
    hi,
    Thanks.But i still don't understand why i get the answer 256 for this program...
    insert
    Code:
    int main()
    {
       unsigned char i=0x80;
       printf("&#37;d",i<<1);
    }
    the decimal equivalent of 0x80 is 128...its bit representation is 1000 0000

    when this is left shifted by 1 all the bits will become zero 0000 0000

    But i get the output 256....

    Can you please explain me how????

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That is because i << 1 when sent to printf is turned into an int. If you do "i <<= 1" or some other operation where you actually STORE the value as a char.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Admittedly, I tend to get confused as well on such things but my guess is that i<<1 returns an int, so indeed 128<<1 gives 256.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    Admittedly, I tend to get confused as well on such things but my guess is that i<<1 returns an int, so indeed 128<<1 gives 256.
    Yes, my first post doesn't explain very well - the expression is "promoted" to integer, because one side is int (1) and the other unsigned char, so the unsigned char is extended to the corresponding unsigned int before the shift is done. Since printf takes any integer type value (less than long) as int, it does never convert back to char.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Aug 2008
    Posts
    17
    hi,
    Thanks great explanation.

    Regards,
    Rohit

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    >> on negative numbers is implementation-defined.
    Sometimes, you get zeros.
    Other times, you get the sign bit.

    Only one of them is arithmetically equivalent to divide by 2.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Feb 2008
    Location
    Bangalore, India
    Posts
    16
    this is happening because of type promotion in C,
    before the shift operation it will convert it to unsigned int than afterword calculation it scale down it in the unsigned or signed char depends on previous nature of object.
    because we are using %d is a type specifier so it will print unsigned int value of the operation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise Operators
    By rrc55 in forum C Programming
    Replies: 6
    Last Post: 04-30-2009, 12:37 PM
  2. bitwise negation problem
    By Mr_Jack in forum C++ Programming
    Replies: 4
    Last Post: 03-15-2004, 08:16 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM
  5. bitwise operators
    By fanaonc in forum C Programming
    Replies: 1
    Last Post: 10-12-2001, 12:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21