Thread: adding 16bit and 32 bit integers

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    19

    adding 16bit and 32 bit integers

    Hi all

    How would i go about creating a function to add 2 16bit and 2 32bit integers together and return a long integer result. i dont even know where to start. Please help

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Code:
    long foo(short a, short b, long c, long d)
    {
       return a+b+c+d;
    }
    I guess the above is what you mean? There is no guarentee that sizeof(short) = 16 bits or sizeof(long) = 32 bits, but they are that size on *nix and MS-windows.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Be careful of overflows. If the result ever becomes larger than 2^32 (or whatever a long is on your system (the ^ is power-of, not XOR in this case)) then your result will be undefined.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed