Thread: bit level hacking?

  1. #1
    Registered User
    Join Date
    Feb 2002

    bit level hacking?

    hey, hows it going?

    i ripped this from the quake 3 source:
    float Q_rsqrt( float number )
       long i;
       float x2, y;
       const float threehalfs = 1.5F;
       x2 = number * 0.5F;
       y  = number;
       i  = * ( long * ) &y;   // evil floating point bit level hacking
       i  = 0x5f3759df - ( i >> 1 );       // what the ..........?
       y  = * ( float * ) &i;
       y  = y * ( threehalfs - ( x2 * y * y ) );   // 1st iteration
       //y  = y * ( threehalfs - ( x2 * y * y ) );   // 2nd iteration, this can be removed
       return y;
    the code returns the reciprocal of sqrt(number).
    my question is this: what do they mean by "bit level hacking"? also, why is 0x5f3759df used?

  2. #2
    Join Date
    Aug 2001
    Groningen (NL)
    >what do they mean by "bit level hacking"?

    They're converting a floating-point variable to a long variable. They get the address of y, cast it to pointer to long and then get the object.

  3. #3
    Nobody seems to know anything about floating point numbers. Everybody knows that they add overhead, but nobody seems to know how they work. You should take the opportunity to find out.

    They're converting it to an integer so that they can manipulate the bits individually. This is what they mean by bit-level-hacking. They want to manipulate the float one bit at a time. They will do this to perform an operation that a float doesn't inherantly allow, or to do it faster than any other method will allow.

    If you don't know how a float is stored internally, try looking up IEEE floating point number format on the web. You may learn a lot about floats just from looking at it. All the bits in a float play a certain ROLE and to manipulate them requires a lot of care (hence the "evil" reference). When you see the format of a float, it may make sense how they would choose an archaic number like that.

    Here's the first link I found on GOOGLE...

    Bear in mind that this may not be the format for the floats in your source code.

  4. #4
    Join Date
    Aug 2001
    Since 0x5f3759df is
    such a strange number you can search for
    it on google.

  5. #5
    Code Warrior
    Join Date
    Nov 2001

    Where the hell did you get the Quake 3 source??
    Last edited by GaPe; 09-07-2002 at 05:30 AM.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  6. #6
    Registered User
    Join Date
    Feb 2002
    Originally posted by GaPe

    Where the hell did you get the Quake 3 source??
    its just the game source, though. they havent released the engine source yet.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 32 bit to 64 bit Ubuntu
    By Akkernight in forum Tech Board
    Replies: 15
    Last Post: 11-17-2008, 02:14 AM
  2. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  3. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 07:02 PM
  4. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM
  5. bit conversions
    By wazilian in forum C Programming
    Replies: 4
    Last Post: 10-25-2001, 08:59 PM