Click here to Skip to main content
Click here to Skip to main content

Fast Algorithm to check the numbers 2^n

By , 12 Dec 2002
Rate this:
Please Sign up or sign in to vote.

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

About the Author

zarzor
Web Developer
Hong Kong Hong Kong
Still alive

Comments and Discussions

 
GeneralC# Trick PinmemberKK Adams30-Jun-08 16:12 
GeneralRe: C# Trick PinmemberMember 150696612-Jun-09 9:36 
GeneralMod - Works for n = 0 Pinmemberst0le18-Feb-08 19:04 
GeneralFFT in Visual C++ PinsussAnonymous7-Jul-03 1:18 
GeneralQuick Mod for 2^0 and others PinmemberColin Davies27-Dec-02 14:16 
GeneralRe: Quick Mod for 2^0 and others Pinmembernutty21-Mar-06 6:23 
GeneralThanks... PinmemberRocky1010-Dec-02 3:36 
GeneralHacker's Delight by Warren PinmemberGeorge V. Reilly9-Dec-02 15:58 
GeneralMake it slightly faster... PinmemberKevinHall5-Dec-02 18:08 
GeneralHmmm.... PinmemberNitron5-Dec-02 17:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 13 Dec 2002
Article Copyright 2002 by zarzor
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid