source: buchla-68k/lib700/ispow2.c@ 0580615

Last change on this file since 0580615 was 0580615, checked in by Thomas Lopatic <thomas@…>, 7 years ago

Point of no return.

  • Property mode set to 100644
File size: 868 bytes
Line 
1/*
2 =============================================================================
3 ispow2.c -- fast test to see if X is a power of 2
4 Version 2 -- 1989-01-24 -- D.N. Lynx Crowe
5
6 Returns:
7
8 TRUE if X is a power of 2,
9 FALSE otherwise.
10 =============================================================================
11*/
12
13#include "stddefs.h"
14
15short ispow2(long x)
16{
17 register short i;
18 register long k;
19
20 k = 0x00000001L; /* setup the bit mask in k */
21
22 for (i = 32; i--; ) { /* check each bit in x ... */
23
24 if ((x & k) NE 0L) { /* ... for the 1st 1 and ... */
25
26 if ((x & ~k) NE 0L) /* ... if there are others ... */
27 return(FALSE); /* ... it's not a power of 2 */
28 else /* ... otherwise .. */
29 return(TRUE); /* ... it is a power of 2 */
30 }
31
32 k = k << 1; /* examine the next position */
33 }
34
35 return(FALSE); /* no bits on isn't a power of 2 */
36}
Note: See TracBrowser for help on using the repository browser.