the magic of MAGIC numbers

This is a discussion on the magic of MAGIC numbers within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Have you ever seen them? The cute little #defines and consts that have weird looking values like 0x81772012 or 0x49122035, ...

  1. #1
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100

    the magic of MAGIC numbers

    Have you ever seen them?

    The cute little #defines and consts that have weird looking values like 0x81772012 or 0x49122035, used allover the code...
    the purpose which is known to only to their author ???

    If you ever had interest in encrypting you're probably acquainted with them
    (Berkely DB has a full load of them! )


    i was wondering how people invent them!
    is it like "hmm let see ... square of 0x1231 minus 0x121909 plus 0x93abde XOR 0x132958 add one more AND VOILA !! thats the number that solves my problems!!! "

    and also if you have some interesting and working code loaded with them share it !
    heres some :
    Code:
    //Berkely DB hashing algorithms (one of the best i've ever seen)
    
    #define	DCHARHASH(h, c)	((h) = 0x63c63cd9*(h) + 0x9c39c33d + (c))
    
    #define UINT8 unsigned char
    
    
    UINT Hashfunc1(const void *key,UINT len)
    {
    	const UINT8 *e, *k;
    	UINT h;
    	UINT8 c;
    
    	k = key;
    	e = k + len;
    	for (h = 0; k != e;) {
    		c = *k++;
    		if (!c && k > e)
    			break;
    		DCHARHASH(h, c);
    	}
    	return h;
    }
    
    
    //////Better
    unsigned int  Hashfunc2(const void *key,UINT len)
    {
    	const unsgined char *e, *k;
    	unsigned int h;
    	k = key;
    	e = k + len;
    	for (h = 0; k < e; ++k) {
    		h *= 16777619; //what is that MAGIC ?!?!?!
    		h ^= *k;
    	}
    	return h;
    }
    Code:
    //author :Praveena <pvncad@krec.ernet.in>
    
    //descr: some encrypting scheme i found 
    //the folowing code is used for generating 
    // key seeds
    #define cypher 8
    #define CYPHER_SZ (8*sizeof(int32_t))
    
    typedef char int8_t;
    typedef short int16_t;
    typedef int int32_t;
    typedef __int64 int64_t;
    
    typedef unsigned char uint8_t;
    typedef unsigned short uint16_t;
    typedef unsigned int uint32_t;
    typedef unsigned __int64 uint64_t;
    
    void modseed(int32_t seed[cypher]) {
    	int x;
    	for(x=1;x<cypher;x++)
    		seed[x]=seed[x]*0x81772012+seed[x]+0x49122035+seed[x+1];
    	for(x=1;x<cypher;x++)
    		seed[0] ^= seed[x];
    }
    int seedsum(int32_t seed[cypher]) {
    	int n,x;
    	
    	n=0x80379251;
    	for(x=0;x<cypher;x++)
    		n=n^seed[x];
    		
    	return ((n>>24)^((n>>16)&255)^((n>>8)&255)^(n&255));
    }
    void seed_gen(char *pwd,int32_t seed[cypher]) {
        char *pp = pwd;
    	memset((void*)seed,0,CYPHER_SZ);
        modseed(seed);
        while((*pp)){
    	modseed(seed);
    	seed[0]=seed[0]+(*pp);
    	modseed(seed);
    	pp++;
        }
    }
    // and encoding
    int encode(char *buf,char *password) {
       int ret = 0;
       char *pstr = buf;
       int seed[cypher];
       seed_gen(password,seed);
       while(*pstr) {
          modseed(seed);
          *pstr ^= (char)(seedsum(seed));
          pstr++;
       }
      *pstr = '\0';
      return 1;
    }

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Those are hexadecimal numbers, base 16, used in memory addresses.
    Truth is a malleable commodity - Dick Cheney

  3. #3
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    0xC000
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  4. #4
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    >>Those are hexadecimal numbers, base 16, used in memory
    >>addresses

    hmm ...
    the fact that they are hex doesn't mean that the author use them for address arithmetics ...
    they could be-bit masks (even more often)

    nobody use constants for memory addressing
    you never assign constant to a pointer you allways use
    [base address + offset]

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    The cute little #defines and consts that have weird looking values like 0x81772012 or 0x49122035, used allover the code...
    the purpose which is known to only to their author ???
    Yes. That is just why people use constants and #defines, to make the code readable and understandable. Usually the constant or #define tells you what the value represents. It often requires more information on how the value was derived.

    >i was wondering how people invent them!

    Depends on the application.

  6. #6
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    that was not what i ment

    people use #defines for many things
    i was fascinated by some code (like Berkely's DB hashing algorithm) in which those numbers make great things

    look at the Berkely DB hashing algorithm!

    Code:
    /*
     * Fowler/Noll/Vo hash
     *
     * The basis of the hash algorithm was taken from an idea sent by email to the
     * IEEE Posix P1003.2 mailing list from Phong Vo (kpv@research.att.com) and
     * Glenn Fowler (gsf@research.att.com).  Landon Curt Noll (chongo@toad.com)
     * later improved on their algorithm.
     *
     * The magic is in the interesting relationship between the special prime
     * 16777619 (2^24 + 403) and 2^32 and 2^8.
     *
     * This hash produces the fewest collisions of any function that we've seen so
     * far, and works well on both numbers and strings.
     *
     * PUBLIC: u_int32_t __ham_func5 __P((DB *, const void *, u_int32_t));
     */
    u_int32_t
    __ham_func5(dbp, key, len)
    	DB *dbp;
    	const void *key;
    	u_int32_t len;
    {
    	const u_int8_t *k, *e;
    	u_int32_t h;
    
    	if (dbp != NULL)
    		COMPQUIET(dbp, NULL);
    
    	k = key;
    	e = k + len;
    	for (h = 0; k < e; ++k) {
    		h *= 16777619;
    		h ^= *k;
    	}
    	return (h);
    }

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >The cute little #defines and consts that have weird looking
    >values like 0x81772012 or 0x49122035, used allover the code...
    >the purpose which is known to only to their author ???
    The purpose is usually well known, and not half as interesting as how the magic number was thought up. For Example, the superblock tag of the BSD sys/fs/ufs_fs.h is

    #define FS_MAGIC 0x011954

    It seems pretty cryptic until you learn that the number is actually the birthday of the author, Kirk McKusick. It gets even stranger, some magic numbers are chosen because they were easily recognized opcodes in the early days of programming.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Prelude, you know too damn much...
    Truth is a malleable commodity - Dick Cheney

  9. #9
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    !!!
    birthday huh!

    so the machine says Happy Birthday To You

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Magic Numbers
    By strokebow in forum Game Programming
    Replies: 12
    Last Post: 11-19-2008, 12:57 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  3. Magic Squares!
    By ZAM777 in forum C++ Programming
    Replies: 1
    Last Post: 04-28-2004, 04:34 PM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21