Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Fast Algorithm to check the numbers 2^n

0.00/5 (No votes)
12 Dec 2002 1  
Optimised code for valid data length to FFT

Introduction

Some applications of signal processing, like Fast Fourier Transform (FFT) need the length of sampled-data input equal to 2^n (where n : integer, n=1,2,3,...), and requires fast testing for this number. This test must be done in a very short time especially in real-time applications operated in DSP cart (as in TMS320C6xxx DSP). Here is an optimized code snippet for this purpose.

Source code

The following function return TRUE if the number x has the form 2^n

bool CheckInputToFFT(int x)
{
    return (!(x & (x-1)));
}

That's all, which means each of the numbers (2,4,8,16,...2^n) gives zero when ANDed with previous number!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here