Determines if integer i is: (i == 0) || (i == pow(2,x)) or calculates 2 to the power of x such that (pow(2,x) >= i) && (pow(2,x-1) < i).
Intop-Power2 | Determines if integer i is: (i == 0) || (i == pow(2,x)) or calculates 2 to the power of x such that (pow(2,x) >= i) && (pow(2,x-1) < i). |
Copyright | This program is free software. |
Files | |
C-kern/ | Header file of Intop-Power2. |
C-kern/ | Implementation file Intop-Power2 impl. |
Functions | |
test | |
unittest_math_int_power2 | Tests ispowerof2_int and makepowerof2_int. |
int_t | |
query | |
ispowerof2_int | Determines if argument is of power of 2 or 0. |
compute | |
makepowerof2_int | Returns power of 2 value of the argument or 0. |
inline implementation | |
Macros | |
ispowerof2_int | Implements int_t.ispowerof2_int as a generic function. |
makepowerof2_int | Implements int_t.makepowerof2_int as a generic function. |
This program is free software. You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
© 2011 Jörg Seebohn
Header file of Intop-Power2.
Implementation file Intop-Power2 impl.
int unittest_math_int_power2( void )
Tests ispowerof2_int and makepowerof2_int.
query | |
ispowerof2_int | Determines if argument is of power of 2 or 0. |
compute | |
makepowerof2_int | Returns power of 2 value of the argument or 0. |
int ispowerof2_int( unsigned i )
Determines if argument is of power of 2 or 0. An integer is of power two if exactly one bit is set. This function is implemented as a generic function for all integer types.
i | Integer argument to be testet |
Every integer of the form (binary) 0..00100..0 is of power of 2 (only one bit is set). If a second bit is set i (==01000100) => (i-1) == 01000011 => (i&(i-1)==01000000) != 0
unsigned makepowerof2_int( unsigned i )
Returns power of 2 value of the argument or 0. The returned value is bigger or equal to the given argument. This function is implemented as a generic function for all integer types.
i | The argument which is transformed into a power of 2 value and returned. |
The returned value is the same as argument i in case of 1. i == 0 2. <ispowerof2_int>(i) is true 3. the next higher power of 2 value can not be represented by this type of integer
Set all bits right of highest to one (00011111111) and then add +1 (00100000000).
Macros | |
ispowerof2_int | Implements int_t.ispowerof2_int as a generic function. |
makepowerof2_int | Implements int_t.makepowerof2_int as a generic function. |
#define ispowerof2_int( i ) (!( (typeof(i)) (((i)-1) & (i)) ))
Implements int_t.ispowerof2_int as a generic function.
Implements int_t.makepowerof2_int as a generic function.
Tests ispowerof2_int and makepowerof2_int.
int unittest_math_int_power2( void )
Implements int_t.ispowerof2_int as a generic function.
#define ispowerof2_int( i ) (!( (typeof(i)) (((i)-1) & (i)) ))
Determines if argument is of power of 2 or 0.
int ispowerof2_int( unsigned i )
Returns power of 2 value of the argument or 0.
unsigned makepowerof2_int( unsigned i )