Thread: Get Exponent of Float

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    17

    Get Exponent of A Float

    Hello,
    Is there a way to get the exponent of a number without using shift and mask operators.

    I can remove the mantissa using division which is equivalent to shifting,
    but I am stuck at masking.

    Code:
    static const int32_t getExponent(uint32_t binary) {
        
        //Shifting
        printf("Exponent = %d\n", ((binary >> 23) & 255) - 127);
        printf("Shift = %d\n", binary >> 23);
        
        // binary = binary / 0b100000000000000000000000
        binary = binary / 8388608;
        printf("Shift by Division = %d\n", binary);
        
        //Masking
        int32_t i = 8, e = 1;
        while (--i) {
            e *= 2;
            if (binary % 2 == 1) {
                ++e;
            }
            binary /= 2;
        }
        return e;
    }
    int main(int argc, const char *argv[]) {
        const float i = 263.3;
        printf("# result = %d\n", getExponent(*(uint32_t *)&i));
        return 0;
    }
    Last edited by ZTik; 05-05-2021 at 06:17 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using an Exponent in C++
    By NismoT in forum C++ Programming
    Replies: 7
    Last Post: 09-26-2011, 09:11 AM
  2. Does anyone know how to do exponent with just addition?
    By cowboyz209 in forum C Programming
    Replies: 11
    Last Post: 05-03-2011, 02:26 PM
  3. Some Exponent Help
    By jrahhali in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-04-2005, 07:31 PM
  4. Exponent help
    By flatline911 in forum C++ Programming
    Replies: 4
    Last Post: 08-15-2003, 01:34 AM
  5. exponent
    By tmoney$ in forum C Programming
    Replies: 2
    Last Post: 04-14-2003, 02:24 PM

Tags for this Thread