@related: [[C]] [[byte]] [[java]] [[algo]] [[bit.fr]] [[bit.en]] [[limit]]
* http://www-graphics.stanford.edu/~seander/bithacks.html
* http://publications.gbdirect.co.uk/c_book/chapter6/bitfields.html
==== ENDIANS ====
#define IS_BIG_ENDIAN (*(uint16_t *)"\0\xff" < 0x100)
* http://msdn.microsoft.com/en-us/library/ms819743.aspx
* http://stackoverflow.com/questions/2100331/c-macro-definition-to-determine-big-endian-or-little-endian-machine
==== bit shifting ====
/// emulate's java's unsigned shift : '>>>'
#define MACRO_BITSHIFT_TYPE(type, value, offset) \
(((offset) > 0) ? \
((((value) >> 1) \
& (~(((type)1) << ((sizeof(value) << 3) - 1)))) >> ((offset) - 1)) \
: ((offset) == 0 ) ? (value) : ((value)<<(offset)) \
)
#define BITSHIFT(value, offset) \
MACRO_BITSHIFT_TYPE(typeof(value), value, offset)
==== power of 2 ====
* http://www.catch22.net/tuts/c-c-tricks
#define ISPOW2(x) (x && !(x & (x-1)))
/// O(1)
#define ISPOW2(x) (((-x)&(x))==(x))
template inline bool ISPOW2(const T &a) { return (a) > 0 && ((a) & ((a)-1)) == 0; }
/// O(32)
#include
int isPower2(unsigned int n) {
if ( ! n ) return n; /* n=0 in register */
for(;;(n>>=1)) { if ( n & 0x1 ) { return ( ! (n>>=1) ); } }
}
int main (int argc, char** argv) {
int r;
unsigned int i=0;
if ( argc >1 ) i = atoi(argv[1]);
r = isPower2( i );
printf("# %d is %sa power of 2\n", i , ( r ) ? "" : "not " );
return r;
}
Endian: 4 char to uint32 :
u32<<=8;
u32|=( buff[i+3] &0xFF);
u32<<=8;
u32|=( buff[i+2] &0xFF);
u32<<=8;
u32|=( buff[i+1] &0xFF);
u32<<=8;
u32|=( buff[i+0] &0xFF);
==== MorE ====
{{http://i.huffpost.com/gadgets/slideshows/246711/slide_246711_1426175_free.jpg}}