// 
// Compares two counters by their least significant byte. (with testing code)
// counters aren't allowed to differ by more than 127 units.
// 
//
//
//
//     This code is helpful if you want to set up your own reliable network communication. TCP/IP
//     does the work, but your program knows when a packet doesn't have to be in the right order or
//     when a lost packet doesn't have to be reacquired. If you want to send a 4 byte counter you only
//     need to send 1 byte. The counter is always comparable to other counters that differ in no
//     more than 127 units.
//
//
// Usage: copy&paste the two functions to your own tool-source-headers (e.g. tools.h)
//
//
//
//     It is not my intention to damage you, but you have to use this at your own risk.
//     This means Iīm not responsible for any damage caused by it. It is prohibited to sell
//     or use this in any commercial way. If you want to publish or present it for downloading,
//     you have to contact me. 
//
//
//     contact: Kosmokleaner@Kosmokleaner.de                         visit: www.Kosmokleaner.de  



#include <stdio.h>




// compare lower than
inline bool CompareL( unsigned char _a, unsigned char _b )          // return( a<b );
{
    int a=(int)_a,b=(int)_b;

    int dist=a-b;

    if(dist>127)b+=256;
        else if(dist<-127)a+=256;

    return(a<b);
}



// compare greater equal
inline bool CompareGE( unsigned char _a, unsigned char _b )         // return( a>=b );
{
    int a=(int)_a,b=(int)_b;

    int dist=a-b;

    if(dist>127)b+=256;
        else if(dist<-127)a+=256;

    return(a>=b);
}







void main( void )
{
    int a,b;

    for(a=0;a<600;a++)
    for(b=0;b<600;b++)
    {
        int dist=a-b;

        if(dist<0)dist=-dist;

        if(dist>127)continue;           // the counter arenīt allowed to differate more that 127 units

        bool r= a<b;

        unsigned char _a=a,_b=b;

        bool _r=CompareL(_a,_b);

        if(r!=_r)       // check for errors
            printf("Error\n");
    }



    for(a=0;a<600;a++)
    for(b=0;b<600;b++)
    {
        int dist=a-b;

        if(dist<0)dist=-dist;

        if(dist>127)continue;           // the counter arenīt allowed to differate more that 127 units

        bool r= a>=b;

        unsigned char _a=a,_b=b;

        bool _r=CompareGE(_a,_b);

        if(r!=_r)       // check for errors
            printf("Error\n");
    }
    
    printf("End of test reached \n");getchar();
}

