Thread: checksum calculation

  1. #1
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64

    checksum calculation

    hi all,
    When I tried to learn about IP checksum I came across the following function for checksum calculation.can any one please explain the code.

    Code:
    u_short
    in_cksum(register u_short *addr, register int len)
    {
        register int nleft = len;
        register u_short *w = addr;
        register u_short answer;
        register int sum = 0;
    
        /*
         *  Our algorithm is simple, using a 32 bit accumulator (sum),
         *  we add sequential 16 bit words to it, and at the end, fold
         *  back all the carry bits from the top 16 bits into the lower
         *  16 bits.
         */
        while (nleft > 1)  {
            sum += *w++;
            nleft -= 2;
        }
    
        /* mop up an odd byte, if necessary */
        if (nleft == 1)
            sum += *(u_char *)w;
    
        /*
         * add back carry outs from top 16 bits to low 16 bits
         */
        sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
        sum += (sum >> 16);         /* add carry */
        answer = ~sum;              /* truncate to 16 bits */
        return (answer);
    }
    Thanks..

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> can any one please explain the code

    The comments weren't enough?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Perhaps there are specific bits you don't understand?
    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.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Is there some other programming language you are more familiar with that we could translate it to perhaps?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making of checksum using XOR
    By Subsonics in forum C Programming
    Replies: 4
    Last Post: 02-20-2009, 01:19 PM
  2. checksum sender vs reciever?
    By silhoutte75 in forum C Programming
    Replies: 1
    Last Post: 04-13-2008, 01:27 PM
  3. weird checksum function
    By sagitt13 in forum C Programming
    Replies: 7
    Last Post: 10-31-2006, 01:25 AM
  4. Help figuring out a checksum algorithm
    By barem23 in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 01-11-2003, 04:24 AM