You Are Here : Home » » Cách viết hàm lũy thừa nhanh nhất

Cách viết hàm lũy thừa nhanh nhất

Hàm pow là hàm có sẵn trong thư viện chuẩn của C <math.h>. Nhưng đây là cách để viết một hàm pow với performance nhanh nhất. Cùng thamg khảo nhé


int ipow(int base, int exp)
{
    int result = 1;
    while (exp)
    {
        if (exp & 1)
            result *= base;
        exp >>= 1;
        base *= base;
    }

    return result;
}

Không có nhận xét nào:

Leave a Reply

Welcome to SpicyTweaks.

Copyright © Embedded System Study. Designed by Momizat Team. Powered to Blogger by SpicyTweaks.

Scroll to top