Thread: Assigning negative number to unsigned variables.

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    1

    Assigning negative number to unsigned variables.

    Code:
    #include<stdio.h>
    main()
    {
            unsigned char a = -125;
            unsigned int b = -125;
            unsigned long c = -125;
            unsigned short d =-125;
            printf(" unsigned char a = %d sizeof a = %d \n", a,sizeof(a));                                                      
            printf("Unsinged int b = %d sizeof b = %d \n",b,sizeof(b));                                                         
            printf("Unsinged long c = %d sizeof c = %d \n",c,sizeof(c));                                                        
            printf("Unsigned short d = %d sizeof d = %d \n",d,sizeof(d));                                                       
    }
    
    
    Output:
    ---------
    
    $ ./a.out                                                                                          
     unsigned char a = 131 sizeof a = 1 
    Unsinged int b = -125 sizeof b = 4 
    Unsinged long c = -125 sizeof c = 4 
    Unsigned short d = 65411 sizeof d = 2 
    
    $
    How does the internal way representation differs from char to int,short,long?

    Could anybody answer?

    -Mahesh.
    Last edited by Salem; 09-26-2006 at 10:32 AM. Reason: Added code tags - learn to use them yourself

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    First post, therefore you get a warning. You aren't using code tags. Now go press edit and fix your post. Read the forum guidelines if you don't understand [code] tags.


    You aren't using the correct format specifier for printf when you try to display unsigned numbers. %u is unsigned. It doesn't matter how it's represented if you display it incorrectly.



    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    A char is usually held in 8 bits, a short in 16 and int and long in 32 bits. If the type is unsigned then the most significant bit can be used to hold part of the value, if it is signed then the most significant bit is a sign bit and is either (0) value is positive or (1) value is negative.

    In your example, you've told the compiler that 'a' is unsigned and to assign a value of -125 to it. So the first thing the compiler is going to do is change -125 to an unsigned char.

    125 in hex is 7D, in binary 0111 1101. To make it negative, you invert each bit and add 1

    0111 1101 -> 1000 0010 + 1 -> 1000 0011 or hex 83

    (1)000 0011 (the sign bit is in brackets).

    If you now treat the same binary number as unsigned, the sign bit becomes part of the number, thus you have hex 83 = decimal 131

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    125 in hex is 7D, in binary 0111 1101. To make it negative, you invert each bit and add 1
    The way numbers are stored internally is implementation-defined, but the way you have described is the most common (I think it's called ones complement or something).

    Not only do you use %u for unsigned variables, as Quzah said, but there's the possiblity that a long is larger than an int. To print an unsigned long int, use
    Code:
    %lu
    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.

  5. #5
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Quote Originally Posted by dwks
    The way numbers are stored internally is implementation-defined, but the way you have described is the most common (I think it's called ones complement or something).
    True, I should have made that clearer. I was trying to explain how -125 became +131 in this particular example.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    Flipping all the bits and adding one gives the two's complement value.
    With one's complement, you don't add the extra one in after flipping all the bits.

    The advantage of two's complement over one's complement is that the subtraction A - B can be achieved by simply adding the two's complement of B to A without any "end around carry", and also there is no redundant value for zero (a binary representation of all ones across the whole variable represents -0 in one's complement).
    Last edited by Driver; 09-27-2006 at 06:03 AM.
    I think you can put a signature here.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Keep in mind that all chars and shorts will be cast to int when passed to printf. Likewise all floats will be passed as doubles.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by King Mir
    Keep in mind that all chars and shorts will be cast to int when passed to printf. Likewise all floats will be passed as doubles.
    Where are you finding this information, because I'm not seeing that? They're all passed as pointers, probably as void pointers. So I doubt the conversion to a double / int on everything. Do you have the reference point in the standard that covers this?


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by quzah
    They're all passed as pointers, probably as void pointers.
    Huh? Parameters to a vararg passed as void pointers?

    I'm pretty sure the standard says absolutely nothing on the topic, but passing them as pointers would be kind of ... pointless.

    Nah, by the typical x86 calling convention, they're indeed passed as simple 32-bit integers.
    Not so sure about the float/double thing though.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CornedBee
    Huh? Parameters to a vararg passed as void pointers?

    I'm pretty sure the standard says absolutely nothing on the topic, but passing them as pointers would be kind of ... pointless.
    My bad. I was thinking scanf. I blame talking on the phone and typing at the same time.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Do you have the reference point in the standard that covers this?
    Section 6.5.2.2, Function calls.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  2. Heap corruption using zlib inflate
    By The Wazaa in forum C++ Programming
    Replies: 0
    Last Post: 03-29-2007, 12:43 PM
  3. 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
  4. can someone check this out and let me know ?
    By javaz in forum C Programming
    Replies: 5
    Last Post: 01-21-2002, 02:13 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM