Thread: unsigned

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    28

    unsigned

    when is the best time to use it.. and what does it really means? i have a gasp definition of it.. but would like to understand it better

  2. #2
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    as you might know , datatypes have a limited number range in memory wich can be found by the formula :

    2^n - 1

    where n is the size of a datatype in bits. An INT for example reserves 2 bytes (4 on some computers). An integer variable can hold any number from : -32,768 to 32,767 wich is the result of :

    2^(2*8) - n

    devided by 2 to cover both negative and positive numbers.

    Now when you precede an INT with the keyword UNSIGNED , that INT will only have positive values ranging from : 0 to 65,535. (didn't devide it by 2 because its only for positive numbers).

    Basically , you use UNSIGNED you need to make a variable big enough to hold your large positive values.

    *EDIT* to figure out how big is a datatype , simply use the sizeof() function. For example :
    Code:
    printf("%d", sizeof(int));
    or
    Code:
    printf("%d", sizeof(unsigned int));
    hope this helped.
    Last edited by Brain Cell; 04-11-2004 at 10:08 PM.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    28
    i understood it.. yet i didnt understand it.. but it still help me out.. cause now i do understand a bit more then before. From what i understand that i didnt know before is that unsigned will always give you a positive number and it should be used when you want to pass large possitive numbers to an array.. if im correct

    p.s

    thank you

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by cigcode
    i understood it.. yet i didnt understand it.. but it still help me out.. cause now i do understand a bit more then before. From what i understand that i didnt know before is that unsigned will always give you a positive number and it should be used when you want to pass large possitive numbers to an array.. if im correct

    p.s

    thank you
    Please don't use the title as an integral part of your question..

    You have a basic grasp of the situation, but where the array part came in is beyond me.

    Signed: positive and negative numbers
    Unsigned: positive numbers only, twice as many as signed.

    That's basically it.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    28
    oh hmm sorry i didnt know i did something wrong.. if i did then sorry about that.. some people in this world take things differently.. like you just did.. thank you anyways

  6. #6
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    if thats true, can you tell me why this works:
    Code:
    unsigned int test = -1;
    
    printf("%d",test);

  7. #7
    Registered User
    Join Date
    Feb 2004
    Posts
    28
    hm i think cause int takes 4 bits of space and all you are doing is removing one as well as unsigned takes 4 bit of space + your value is a low value.. and unsigned works on large ones

    but then again i could be wrong.. cause im still learning and still new to c.. so please if im wrong i would like to be corrected

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The reason
    Code:
    unsigned int test = -1;
    printf("%d",test);
    works is because of this: Everything is reprensented as a bit pattern, only thing that changes is how we interpet that bit pattern and that determines what it is.

    Because bit patterns have no particular way to represent negitive number, by convention we have determined a way to interpet them so that we can represent negitive numbers.

    The bit pattern for UINT_MAX (for a 32 bit system) is:
    11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111
    and the bit pattern for -1 is ( for a 32 bit system)
    11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111

    The difference? Just how we interpet the bit pattern.

    when you use "%d" in printf we tell the function to interpet the bit pattern as signed so we get -1, if we had used "%u" we would have gotten 4294967295.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Signed: positive and negative numbers
    >Unsigned: positive numbers only, twice as many as signed.
    >
    >That's basically it.
    Ah, but you forget overflow guarantees. Signed overflow is guaranteed to be undefined, whereas unsigned overflow is guaranteed to have a result that remains within the bounds of the type size by calculating modulo 2^n and rolling over. So
    Code:
    unsigned int test = -1;
    Is equivalent to UINT_MAX because of overflow toward the negative.

    >if thats true, can you tell me why this works
    You got lucky? If the type of the value and the format flag to printf designating the type do not match, the behavior is undefined. That includes unsigned qualifiers for otherwise equivalent types.

    >unsigned will always give you a positive number and it should be used when you want to pass large possitive numbers to an array
    No, not really. It's possible to legally use negative indices into an array because subtraction is legal in pointer arithmetic. As long as you dereference memory that you own, all is well:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      char *s = "test";
    
      s++;
      printf ( "%c\n", s[-1] );
    
      return 0;
    }
    However, for the most part you're correct, and the standard even gives us a type definition that is perfect for array indices, size_t.

    >An INT for example reserves 2 bytes (4 on some computers).
    Or more. The size of an int must be at least 16 bits, but there is no restriction on it being larger.

    >An integer variable can hold any number from : -32,768 to 32,767
    Not for all bit representations. To be absolutely correct and portable, a 16 bit integer can hold any value from -32,767 to 32,767.
    My best code is written with the delete key.

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by Thantos
    The bit pattern for UINT_MAX (for a 32 bit system) is:
    11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111
    and the bit pattern for -1 is ( for a 32 bit system)
    11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111
    So in a "UINT_MAX (for a 32 bit system)," how do you cram those 64 bits into 32?



    Quote Originally Posted by Prelude
    >Signed: positive and negative numbers
    >Unsigned: positive numbers only, twice as many as signed.
    >
    >That's basically it.
    Ah, but you forget overflow guarantees.
    Well, since I was answering a noob, I decided not to give the entire disertation on the concept of numerical values at this point.

    My motto:
    "Let them get comfortable with the simple. We can confuse them with complex details later. This way their heads won't explode"
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    how do you cram those 64 bits into 32?
    That is a special property of law that occurs at 11:12 PM when one is really tired.

  12. #12
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by Thantos
    That is a special property of law that occurs at 11:12 PM when one is really tired.
    Ahh, yes. We must be in a different time zone. That occurrs for me around 4:30AM
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. Heap corruption using zlib inflate
    By The Wazaa in forum C++ Programming
    Replies: 0
    Last Post: 03-29-2007, 12:43 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM