Last change
on this file since 9519422 was 7258c6a, checked in by Thomas Lopatic <thomas@…>, 7 years ago |
Use standard integer types.
|
-
Property mode
set to
100644
|
File size:
878 bytes
|
Rev | Line | |
---|
[f40a309] | 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 |
|
---|
[7258c6a] | 15 | int16_t ispow2(int32_t x)
|
---|
[f40a309] | 16 | {
|
---|
[7258c6a] | 17 | register int16_t i;
|
---|
| 18 | register int32_t k;
|
---|
[f40a309] | 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.