September 13, 2023

  • Mathematical mind

    Write a max function without using any conditional statements.

    union FloatUint
    {
    float f;
    uint32_t n;
    };

    float max(float a, float b)
    {
    float t[2] = { a, b };
    FloatUint c;
    c.f = a - b;
    return t[c.u >> 31];
    }

    float max(float a, float b)
    {
    return (a + b) / 2 + abs((a - b) / 2);
    }

    Write a program that takes an input x and returns 1 if x is 2, and 2 if x is 1.

    return 3 – x;
    or
    return x XOR 3;

Post a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *