12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- // Bits/bytes manipulation tools
- //
- // Platform: ISO C++ 98/11
- // $Id$
- //
- // (c) __vic 2011
- #ifndef __VIC_BITS_H
- #define __VIC_BITS_H
- #include<__vic/defs.h>
- #include<__vic/stdint.h>
- #include<climits>
- namespace __vic {
- //----------------------------------------------------------------------------
- // Returns low-order nibble (or tetrad, or half-byte) of the byte
- __VIC_CONSTEXPR_FUNC int lo_nibble(uint8_t b)
- {
- return b & 0x0F;
- }
- //----------------------------------------------------------------------------
- // Returns high-order nibble (or tetrad, or half-byte) of the byte
- __VIC_CONSTEXPR_FUNC int hi_nibble(uint8_t b)
- {
- return b >> 4;
- }
- //------------------------------------------------------------------------------
- // Returns value with all most significant bits are 1 (others - 0)
- // Parameter specifies the number of "ones"
- template<class T>
- inline T msb_ones(unsigned bits_num)
- {
- return ~T(0) << (sizeof(T) * CHAR_BIT - bits_num);
- }
- //------------------------------------------------------------------------------
- // Returns value with all least significant bits are 1 (others - 0)
- // Parameter specifies the number of "ones"
- template<class T>
- inline T lsb_ones(unsigned bits_num)
- {
- return ~(~T(0) << bits_num);
- }
- //------------------------------------------------------------------------------
- // Clears all but bits_num least significant bits
- template<class T>
- inline T get_lsbs(T v, unsigned bits_num)
- {
- return v & lsb_ones<T>(bits_num);
- }
- //----------------------------------------------------------------------------
- // Returns char code 0..255
- __VIC_CONSTEXPR_FUNC int ord(char ch)
- {
- return static_cast<unsigned char>(ch);
- }
- //----------------------------------------------------------------------------
- // Swap high-order nibble with low-order one
- __VIC_CONSTEXPR_FUNC uint8_t swapped_nibbles(uint8_t b)
- {
- return (b << 4) | (b >> 4);
- }
- //----------------------------------------------------------------------------
- template<class UInt>
- inline UInt rotl_uint(UInt v, int shift)
- {
- return (v << shift) | (v >> (sizeof(UInt) * CHAR_BIT - shift));
- }
- //----------------------------------------------------------------------------
- template<class UInt>
- inline UInt rotr_uint(UInt v, int shift)
- {
- return (v >> shift) | (v << (sizeof(UInt) * CHAR_BIT - shift));
- }
- //----------------------------------------------------------------------------
- inline uint8_t rotl(uint8_t v, int sh) { return rotl_uint(v, sh); }
- inline uint16_t rotl(uint16_t v, int sh) { return rotl_uint(v, sh); }
- inline uint32_t rotl(uint32_t v, int sh) { return rotl_uint(v, sh); }
- inline uint64_t rotl(uint64_t v, int sh) { return rotl_uint(v, sh); }
- //----------------------------------------------------------------------------
- inline uint8_t rotr(uint8_t v, int sh) { return rotr_uint(v, sh); }
- inline uint16_t rotr(uint16_t v, int sh) { return rotr_uint(v, sh); }
- inline uint32_t rotr(uint32_t v, int sh) { return rotr_uint(v, sh); }
- inline uint64_t rotr(uint64_t v, int sh) { return rotr_uint(v, sh); }
- //----------------------------------------------------------------------------
- } // namespace
- #endif // header guard
|