Thread: unsigned int and unsigned long int

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    unsigned int and unsigned long int

    what is the difference?? one is used for 32 bit machine and one is for 64 bit machine?? say that I have a number in 32 bit and I want to convert it into 64 bit, how do I do this?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    On a 32-bit machine, int and long are (most likely) the same size. On a 64-bit machine, long may be 32 or 64-bit depending on the choice of the system designers.

    Many compilers support "long long", which is 64-bit in most architectures.

    --
    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.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    say that I have a long long int, that would be useful if I use it in a 32 bit machine right??

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Maybe and maybe not. It depends on the compiler.
    Long long int is 32-bit in Visual Studio, for example.
    I'd say use the (u)intXX_t types instead. For example,
    uint32_t - Unsigned 32-bit integer.
    uint64_t - Unsigned 64-bit integer.

    No confusion about the size.
    I really don't understand why they couldn't have added such types to the standard and locked them in place.
    It's supposed to be in a header called stdint.h.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so if I would want to do a program that extracts the sign mantissa and exponent from a 32 or 64 bit machine, and I specify this on my sizes.h, will this be right??

    #if defined(MACHINE32)
    typedef int int_4;
    typedef unsigned int int_u4;
    typedef long long int int_8;
    typedef unsigned long long int int_u8;
    #elif defined(MACHINE64)
    typedef int int int_4;
    typedef unsigned int int_u4;
    typedef long long int int_8;
    typedef unsigned long long int int_u8;
    #else
    #error 666
    #endif
    Last edited by -EquinoX-; 03-05-2008 at 12:03 PM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    int8_t for signed
    uint8_t for unsigned

    Basically "u" is for unsigned, then it's "int", then you specify the size in bits and then add "_t" and you have four type.
    Say you want an unsigned integer of 16 bits. That would be:
    uint16_t
    Want a signed one?
    int16_t
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by -EquinoX- View Post
    so if I would want to do a program that extracts the sign mantissa and exponent from a 32 or 64 bit machine, and I specify this on my sizes.h, will this be right??

    #if defined(MACHINE32)
    typedef int int_4;
    typedef unsigned int int_u4;
    typedef long long int int_8;
    typedef unsigned long long int int_u8;
    #elif defined(MACHINE64)
    typedef int int int_4;
    typedef unsigned int int_u4;
    typedef long long int int_8;
    typedef unsigned long long int int_u8;
    #else
    #error 666
    #endif
    No. A typedef creates an alias for an existing type. (Deja vu?) Even the uintN_t are optional additions to C99.

    Use a type whose range is appropriate for the value it is supposed to hold.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so if I want to have an int_4 or int_u8 in a 64 bit machine, how can I do that?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Firstly, I'd still suggest using the (u)intxx_t types, and secondly, the types will be right if compiled as 64-bit code.
    You just use (u)int32_t and (u)int64_t where appropriate in your code and you will get 32-bit and 64-bit types regardless if it's a 64-bit machine or a 32-bit machine.
    Of course, your code needs to be compiled as a 64-bit app, as well, otherwise it won't work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    but is there any other alternative to that to what the general 64 bit for a 4 byte unsigned int for example??

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    These types are defined or typedefed to the appropriate type, so for example the uint32_t will always be 32-bits regardless of machine.
    I think that's what you're asking?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    what if I am asked to create my own type defined here

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Elysia View Post
    You just use (u)int32_t and (u)int64_t where appropriate in your code and you will get 32-bit and 64-bit types regardless if it's a 64-bit machine or a 32-bit machine.
    Of course, your code needs to be compiled as a 64-bit app, as well, otherwise it won't work.
    The idea is just opposite - independently of computer/compilation type int32 will be 32 bit type and int64 will be 64 bit type even when compiled in 32-bit mode on 32 computer/OS
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Quote Originally Posted by Elysia View Post
    Long long int is 32-bit in Visual Studio, for example.
    I did not test if this is true (and i don't know which version of VS you are talking about, but i'm quite sure the 2005 long long int type is 64-bit) but the standard (C99) says that the long long int type should be able to store at least a 64-bit value.

    Of course, few compilers are 100% standard-compliant.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, that's what I was trying to imply. I think I made a mistake there.

    Quote Originally Posted by foxman View Post
    I did not test if this is true (and i don't know which version of VS you are talking about, but i'm quite sure the 2005 long long int type is 64-bit) but the standard (C99) says that the long long int type should be able to store at least a 64-bit value.

    Of course, few compilers are 100% standard-compliant.
    Visual Studio is not C99 compliant and does not support long long. They are merely 32-bit.
    __int64 is used for 64-bit integers in Visual Studio.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM