Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

bits_n_bytes.h File Reference


Detailed Description

Useful bit/byte macros for things like counting bits, swapping bytes, etc.

NOTE: gcc (perhaps other compilers) have 'built-in' functions for optimal implementation of many of these macros. Check the documentation.

Go to the source code of this file.

Defines

#define BYTEINWORD32(word, byteasword)
#define COUNTBITS16(b)
#define COUNTBITS32(b)
#define COUNTBITS64(b)
#define REVERSEBITS16(b)
#define REVERSEBITS32(b)
#define REVERSEBITS64(b)
#define SWAPBYTES16(word)
#define SWAPBYTES32(word)


Define Documentation

#define BYTEINWORD32 (  word,
byteasword   ) 
 

Value:

(\
(((word)&0x000000ff)         == (byteasword)) || \
((((word)&0x0000ff00) >> 8)  == (byteasword)) || \
((((word)&0x00ff0000) >> 16) == (byteasword)) || \
((((word)&0xff000000) >> 24) == (byteasword)) \
)

#define COUNTBITS16 (  b   ) 
 

Value:

(\
        (b)=(((b)>> 1)&0x5555)+((b)&0x5555),\
        (b)=(((b)>> 2)&0x3333)+((b)&0x3333),\
        (b)=(((b)>> 4)&0x0707)+((b)&0x0707),\
        (b)=(((b)>> 8)&0x000f)+((b)&0x000f),\
        (b))

#define COUNTBITS32 (  b   ) 
 

Value:

(\
        (b)=(((b)>> 1)&0x55555555)+((b)&0x55555555),\
        (b)=(((b)>> 2)&0x33333333)+((b)&0x33333333),\
        (b)=(((b)>> 4)&0x07070707)+((b)&0x07070707),\
        (b)=(((b)>> 8)&0x000f000f)+((b)&0x000f000f),\
        (b)=(((b)>>16)&0x0000001f)+((b)&0x0000001f),\
        (b))

#define COUNTBITS64 (  b   ) 
 

Value:

(\
        (b)=(((b)>> 1)&0x5555555555555555)+((b)&0x5555555555555555),\
        (b)=(((b)>> 2)&0x3333333333333333)+((b)&0x3333333333333333),\
        (b)=(((b)>> 4)&0x0707070707070707)+((b)&0x0707070707070707),\
        (b)=(((b)>> 8)&0x000f000f000f000f)+((b)&0x000f000f000f000f),\
        (b)=(((b)>>16)&0x0000001f0000001f)+((b)&0x0000001f0000001f),\
        (b)=(((b)>>32)&0x000000000000003f)+((b)&0x000000000000003f),\
        (b))

#define REVERSEBITS16 (  b   ) 
 

Value:

(\
        (b)=(((b)>> 1)&0x5555)|(((b)&0x5555)<< 1),\
        (b)=(((b)>> 2)&0x3333)|(((b)&0x3333)<< 2),\
        (b)=(((b)>> 4)&0x0f0f)|(((b)&0x0f0f)<< 4),\
        (b)=(((b)>> 8)&0x00ff)|(((b)&0x00ff)<< 8),\
        (b))

#define REVERSEBITS32 (  b   ) 
 

Value:

(\
        (b)=(((b)>> 1)&0x55555555)|(((b)&0x55555555)<< 1),\
        (b)=(((b)>> 2)&0x33333333)|(((b)&0x33333333)<< 2),\
        (b)=(((b)>> 4)&0x0f0f0f0f)|(((b)&0x0f0f0f0f)<< 4),\
        (b)=(((b)>> 8)&0x00ff00ff)|(((b)&0x00ff00ff)<< 8),\
        (b)=(((b)>>16)&0x0000ffff)|(((b)&0x0000ffff)<<16),\
        (b))

#define REVERSEBITS64 (  b   ) 
 

Value:

(\
        (b)=(((b)>> 1)&0x5555555555555555)|(((b)&0x5555555555555555)<< 1),\
        (b)=(((b)>> 2)&0x3333333333333333)|(((b)&0x3333333333333333)<< 2),\
        (b)=(((b)>> 4)&0x0f0f0f0f0f0f0f0f)|(((b)&0x0f0f0f0f0f0f0f0f)<< 4),\
        (b)=(((b)>> 8)&0x00ff00ff00ff00ff)|(((b)&0x00ff00ff00ff00ff)<< 8),\
        (b)=(((b)>>16)&0x0000ffff0000ffff)|(((b)&0x0000ffff0000ffff)<<16),\
        (b)=(((b)>>32)&0x00000000ffffffff)|(((b)&0x00000000ffffffff)<<32),\
        (b))

#define SWAPBYTES16 (  word   ) 
 

Value:

(\
(((word)&0x00ff) << 8) | \
(((word)&0xff00) >> 8) \
)

#define SWAPBYTES32 (  word   ) 
 

Value:

(\
(((word)&0x000000ff) << 24)| \
(((word)&0x0000ff00) << 8) | \
(((word)&0x00ff0000) >> 8) | \
(((word)&0xff000000) >> 24) \
)

Aware 0.11.1 Copyright (C) 1998-2005 Russell Leighton (russ@elegant-software.com)