bits.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Bits/bytes manipulation tools
  2. //
  3. // Platform: ISO C++ 98/11
  4. // $Id$
  5. //
  6. // (c) __vic 2011
  7. #ifndef __VIC_BITS_H
  8. #define __VIC_BITS_H
  9. #include<__vic/defs.h>
  10. #include<__vic/stdint.h>
  11. #include<climits>
  12. namespace __vic {
  13. //----------------------------------------------------------------------------
  14. // Returns low-order nibble (or tetrad, or half-byte) of the byte
  15. __VIC_CONSTEXPR_FUNC int lo_nibble(uint8_t b)
  16. {
  17. return b & 0x0F;
  18. }
  19. //----------------------------------------------------------------------------
  20. // Returns high-order nibble (or tetrad, or half-byte) of the byte
  21. __VIC_CONSTEXPR_FUNC int hi_nibble(uint8_t b)
  22. {
  23. return b >> 4;
  24. }
  25. //------------------------------------------------------------------------------
  26. // Returns value with all most significant bits are 1 (others - 0)
  27. // Parameter specifies the number of "ones"
  28. template<class T>
  29. inline T msb_ones(unsigned bits_num)
  30. {
  31. return ~T(0) << (sizeof(T) * CHAR_BIT - bits_num);
  32. }
  33. //------------------------------------------------------------------------------
  34. // Returns value with all least significant bits are 1 (others - 0)
  35. // Parameter specifies the number of "ones"
  36. template<class T>
  37. inline T lsb_ones(unsigned bits_num)
  38. {
  39. return ~(~T(0) << bits_num);
  40. }
  41. //------------------------------------------------------------------------------
  42. // Clears all but bits_num least significant bits
  43. template<class T>
  44. inline T get_lsbs(T v, unsigned bits_num)
  45. {
  46. return v & lsb_ones<T>(bits_num);
  47. }
  48. //----------------------------------------------------------------------------
  49. // Returns char code 0..255
  50. __VIC_CONSTEXPR_FUNC int ord(char ch)
  51. {
  52. return static_cast<unsigned char>(ch);
  53. }
  54. //----------------------------------------------------------------------------
  55. // Swap high-order nibble with low-order one
  56. __VIC_CONSTEXPR_FUNC uint8_t swapped_nibbles(uint8_t b)
  57. {
  58. return (b << 4) | (b >> 4);
  59. }
  60. //----------------------------------------------------------------------------
  61. template<class UInt>
  62. inline UInt rotl_uint(UInt v, int shift)
  63. {
  64. return (v << shift) | (v >> (sizeof(UInt) * CHAR_BIT - shift));
  65. }
  66. //----------------------------------------------------------------------------
  67. template<class UInt>
  68. inline UInt rotr_uint(UInt v, int shift)
  69. {
  70. return (v >> shift) | (v << (sizeof(UInt) * CHAR_BIT - shift));
  71. }
  72. //----------------------------------------------------------------------------
  73. inline uint8_t rotl(uint8_t v, int sh) { return rotl_uint(v, sh); }
  74. inline uint16_t rotl(uint16_t v, int sh) { return rotl_uint(v, sh); }
  75. inline uint32_t rotl(uint32_t v, int sh) { return rotl_uint(v, sh); }
  76. inline uint64_t rotl(uint64_t v, int sh) { return rotl_uint(v, sh); }
  77. //----------------------------------------------------------------------------
  78. inline uint8_t rotr(uint8_t v, int sh) { return rotr_uint(v, sh); }
  79. inline uint16_t rotr(uint16_t v, int sh) { return rotr_uint(v, sh); }
  80. inline uint32_t rotr(uint32_t v, int sh) { return rotr_uint(v, sh); }
  81. inline uint64_t rotr(uint64_t v, int sh) { return rotr_uint(v, sh); }
  82. //----------------------------------------------------------------------------
  83. } // namespace
  84. #endif // header guard