| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

bit_utility.h

Go to the documentation of this file.
00001 // portable functions for endian swap.
00002 // in replace of ntohl()...
00003 #include <stdint.h> // uint32_t
00004 
00005 inline void endian_swap(unsigned short& x)
00006 {
00007    x = (x >> 8) |
00008        (x << 8);
00009 }
00010 
00011 inline void endian_swap(unsigned int& x)
00012 {
00013    x = (x >> 24) |
00014        ((x << 8) & 0x00FF0000) |
00015        ((x >> 8) & 0x0000FF00) |
00016        (x << 24);
00017 }
00018 
00019 /*
00020 // __int64 for MSVC, "long long" for gcc
00021 inline void endian_swap(unsigned __int64& x)
00022 {
00023     x = (x>>56) |
00024         ((x<<40) & 0x00FF000000000000) |
00025         ((x<<24) & 0x0000FF0000000000) |
00026         ((x<<8)  & 0x000000FF00000000) |
00027         ((x>>8)  & 0x00000000FF000000) |
00028         ((x>>24) & 0x0000000000FF0000) |
00029         ((x>>40) & 0x000000000000FF00) |
00030         (x<<56);
00031 }
00032 */
00033 
00036 inline unsigned int get_bits(const uint32_t& word, const unsigned int& offset, const unsigned int& nbits)
00037 {
00038    uint32_t mask = 0;
00039    for (unsigned int i = 0; i < nbits; i++)
00040       mask = (mask << 1) + 1;
00041    return (word >> offset) & mask;
00042 }
00043 
00046 inline bool test_bit(const uint32_t& word, const unsigned int& bit)
00047 {
00048    uint32_t mask=1;
00049    mask = mask << bit;
00050    return (word & mask) == mask;
00051 }
00052 
00055 inline unsigned int bcd2dec(const unsigned int code) 
00056 {
00057   unsigned int dec = 0;
00058   unsigned int bcd = code;
00059   int index = 1;
00060   while(bcd != 0) {
00061     dec += (bcd & 0xf) * index;
00062     bcd = bcd >> 4;
00063     index *= 10;
00064   }
00065   return dec;
00066 }
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Mon Apr 11 20:27:02 2011 for RawDataIO by doxygen 1.4.7