Last change
on this file since 6d3de83 was 3ae31e9, checked in by Thomas Lopatic <thomas@…>, 8 years ago |
Imported original source code.
|
-
Property mode
set to
100755
|
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 |
|
---|
15 | short
|
---|
16 | ispow2(x)
|
---|
17 | register 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.