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

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

Compiled full ROM in Hatari.

  • Property mode set to 100644
File size: 918 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
16ispow2(x)
17register long x;
18{
19 register short i;
20 register long k;
21
22 k = 0x00000001L; /* setup the bit mask in k */
23
24 for (i = 32; i--; ) { /* check each bit in x ... */
25
26 if ((x & k) NE 0L) { /* ... for the 1st 1 and ... */
27
28 if ((x & ~k) NE 0L) /* ... if there are others ... */
29 return(FALSE); /* ... it's not a power of 2 */
30 else /* ... otherwise .. */
31 return(TRUE); /* ... it is a power of 2 */
32 }
33
34 k = k << 1; /* examine the next position */
35 }
36
37 return(FALSE); /* no bits on isn't a power of 2 */
38}
Note: See TracBrowser for help on using the repository browser.