• Quibblekrust@thelemmy.club
    link
    fedilink
    English
    arrow-up
    5
    ·
    2 days ago

    It’s much better to make your own function that uses bitwise operations to do addition.

    function add(a, b) {
        while (b !== 0) {
            // Calculate carry
            let carry = a & b;
    
            // Sum without carry
            a = a ^ b;
    
            // Shift carry to the left
            b = carry << 1;
        }
        return a;
    }
    

    (For certain definitions of better.)