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).

Summary
Intop-Power2Determines 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).
CopyrightThis program is free software.
Files
C-kern/api/math/int/power2.hHeader file of Intop-Power2.
C-kern/math/int/power2.cImplementation file Intop-Power2 impl.
Functions
test
unittest_math_int_power2Tests ispowerof2_int and makepowerof2_int.
int_t
query
ispowerof2_intDetermines if argument is of power of 2 or 0.
compute
makepowerof2_intReturns power of 2 value of the argument or 0.
inline implementation
Macros
ispowerof2_intImplements int_t.ispowerof2_int as a generic function.
makepowerof2_intImplements int_t.makepowerof2_int as a generic function.

Copyright

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.

Author

© 2011 Jörg Seebohn

Files

C-kern/api/math/int/power2.h

Header file of Intop-Power2.

C-kern/math/int/power2.c

Implementation file Intop-Power2 impl.

test

unittest_math_int_power2

int unittest_math_int_power2(void)

Tests ispowerof2_int and makepowerof2_int.

int_t

Summary
query
ispowerof2_intDetermines if argument is of power of 2 or 0.
compute
makepowerof2_intReturns power of 2 value of the argument or 0.

query

ispowerof2_int

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.

Parameter

iInteger 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

compute

makepowerof2_int

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.

Parameter

iThe 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

Algorithm

Set all bits right of highest to one (00011111111) and then add +1 (00100000000).

inline implementation

Summary
Macros
ispowerof2_intImplements int_t.ispowerof2_int as a generic function.
makepowerof2_intImplements int_t.makepowerof2_int as a generic function.

Macros

ispowerof2_int

#define ispowerof2_int(i) (!( (typeof(i)) (((i)-1) & (i)) ))

Implements int_t.ispowerof2_int as a generic function.

makepowerof2_int

Implements int_t.makepowerof2_int as a generic function.

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).
Implement unittest_generic_bits_power2.
int unittest_math_int_power2(void)
Tests ispowerof2_int and makepowerof2_int.
#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.
int ispowerof2_int(unsigned i)
Determines if argument is of power of 2 or 0.
unsigned makepowerof2_int(unsigned i)
Returns power of 2 value of the argument or 0.
Close