📄 gbits.h
字号:
#ifndef __GBITS_H__#define __GBITS_H__#include <stdlib.h>#include "GMacros.h"#define BITS_PER_BYTE 8#define BITS_PER_UINT (BITS_PER_BYTE * sizeof(unsigned int))class GBits{public: // Convert a number to its Gray code encoding static inline unsigned int BinaryToGrayCode(unsigned int nBinary) { return nBinary ^ (nBinary >> 1); } // Convert a number in Gray code encoding to a value static inline unsigned int GrayCodeToBinary(unsigned int nGrayCode) { unsigned int nMask = nGrayCode >> 1; while(nMask > 0) { nGrayCode ^= nMask; nMask >>= 1; } return nGrayCode; } // Returns true if a number is a power of two static inline bool IsPowerOfTwo(unsigned int n) { return ((n & (n - 1)) == 0); } // Returns the sign (-1, 0, +1) of an integer static inline int GetSign(int n) { if(n > 0) return 1; if(n < 0) return -1; return 0; } static inline int Round(float f) { if(f >= 0) return (int)(f + (float).5); else return (int)(f - (float).5); } static inline int RoundUp(float f) { if(f >= 0) return (int)f + 1; else return (int)f; } static inline int RoundDown(float f) { if(f >= 0) return (int)f; else return (int)f - 1; } // Returns a random unsigned integer between 0 and 0xffffffff inclusively static inline unsigned int GetRandomUint() { return (((unsigned int)rand() % 0xfff) << 20) | (((unsigned int)rand() % 0xfff) << 8) | ((unsigned int)rand() % 0xff); } // Returns a random double between 0 and 1 static inline double GetRandomDouble() { return (double)rand() / RAND_MAX; } static void DoubleToString(char* szBuf, double d) { sprintf(szBuf, "%f", d); // Find the decimal point int i; for(i = 0; szBuf[i] != '\0'; i++) { if(szBuf[i] == '.') break; } if(szBuf[i] == '\0') return; // Clip trailing zeros for( ; szBuf[i] != '\0'; i++) { } for(i--; i > 0; i--) { if(szBuf[i] == '0') szBuf[i] = '\0'; else if(szBuf[i] == '.') { szBuf[i] = '\0'; break; } else break; } } // Converts two hexadecimal digits to a byte. h1 is least significant // digit. h2 is most significant digit. static inline unsigned char HexToByte(char h1, char h2) { char v1, v2; if(h1 <= '9') v1 = h1 - '0'; else if(h1 <= 'Z') v1 = h1 - 'A' + 10; else v1 = h1 - 'a' + 10; if(h2 <= '9') v2 = h2 - '0'; else if(h2 <= 'Z') v2 = h2 - 'A' + 10; else v2 = h2 - 'a' + 10; GAssert(v1 >= 0 && v1 <= 15 && v2 >= 0 && v2 <= 15, "bad hex character"); return(v1 | (v2 << 4)); } // Converts a byte to two hex digits. The least significate digit // will come first. the most significant digit comes next. static inline void ByteToHex(unsigned char byte, char* pHex) { pHex[0] = (byte & 15) + '0'; if(pHex[0] > '9') pHex[0] += ('a' - '0' - 10); pHex[1] = (byte >> 4) + '0'; if(pHex[1] > '9') pHex[1] += ('a' - '0' - 10); } // pHex should point to a buffer that is at 2 * nBufferSize + 1 static void BufferToHex(const unsigned char* pBuffer, int nBufferSize, char* pHexTwiceAsLarge) { int n; for(n = 0; n < nBufferSize; n++) ByteToHex(pBuffer[n], &pHexTwiceAsLarge[n << 1]); pHexTwiceAsLarge[2 * n] = '\0'; } // pBuffer should be half the size of nHexSize static void HexToBuffer(const char* pHex, int nHexSize, unsigned char* pBuffer) { GAssert(nHexSize % 2 == 0, "not a multiple of 2"); nHexSize /= 2; int n; for(n = 0; n < nHexSize; n++) pBuffer[n] = HexToByte(pHex[2 * n], pHex[2 * n + 1]); } // Convert a 32-bit native integer to little endian static inline unsigned int N32ToLittleEndian(unsigned int in) {#ifdef BYTE_ORDER_BIG_ENDIAN return ReverseEndian(in);#else // BYTE_ORDER_BIG_ENDIAN return in;#endif // !BYTE_ORDER_BIG_ENDIAN } // Convert a 32-bit native integer to little endian static inline int N32ToLittleEndian(int in) {#ifdef BYTE_ORDER_BIG_ENDIAN return ReverseEndian(in);#else // BYTE_ORDER_BIG_ENDIAN return in;#endif // !BYTE_ORDER_BIG_ENDIAN } // Convert a 16-bit native integer to little endian static inline unsigned short N16ToLittleEndian(unsigned short in) {#ifdef BYTE_ORDER_BIG_ENDIAN return ReverseEndian(in);#else // BYTE_ORDER_BIG_ENDIAN return in;#endif // !BYTE_ORDER_BIG_ENDIAN } // Convert a 16-bit native integer to little endian static inline short N16ToLittleEndian(short in) {#ifdef BYTE_ORDER_BIG_ENDIAN return ReverseEndian(in);#else // BYTE_ORDER_BIG_ENDIAN return in;#endif // !BYTE_ORDER_BIG_ENDIAN } // Convert a 32-bit native float to little endian static inline float R32ToLittleEndian(float in) {#ifdef BYTE_ORDER_BIG_ENDIAN return ReverseEndian(in);#else // BYTE_ORDER_BIG_ENDIAN return in;#endif // !BYTE_ORDER_BIG_ENDIAN } // Convert a 64-bit native float to little endian static inline double R64ToLittleEndian(double in) {#ifdef BYTE_ORDER_BIG_ENDIAN return ReverseEndian(in);#else // BYTE_ORDER_BIG_ENDIAN return in;#endif // !BYTE_ORDER_BIG_ENDIAN } // Convert little endian to a 32-bit native integer static inline unsigned int LittleEndianToN32(unsigned int in) {#ifdef BYTE_ORDER_BIG_ENDIAN return ReverseEndian(in);#else // BYTE_ORDER_BIG_ENDIAN return in;#endif // !BYTE_ORDER_BIG_ENDIAN } // Convert little endian to a 32-bit native integer static inline int LittleEndianToN32(int in) {#ifdef BYTE_ORDER_BIG_ENDIAN return ReverseEndian(in);#else // BYTE_ORDER_BIG_ENDIAN return in;#endif // !BYTE_ORDER_BIG_ENDIAN } // Convert little endian to a 16-bit native integer static inline unsigned short LittleEndianToN16(unsigned short in) {#ifdef BYTE_ORDER_BIG_ENDIAN return ReverseEndian(in);#else // BYTE_ORDER_BIG_ENDIAN return in;#endif // !BYTE_ORDER_BIG_ENDIAN } // Convert little endian to a 16-bit native integer static inline short LittleEndianToN16(short in) {#ifdef BYTE_ORDER_BIG_ENDIAN return ReverseEndian(in);#else // BYTE_ORDER_BIG_ENDIAN return in;#endif // !BYTE_ORDER_BIG_ENDIAN } // Convert little endian to a 32-bit native float static inline float LittleEndianToR32(float in) {#ifdef BYTE_ORDER_BIG_ENDIAN return ReverseEndian(in);#else // BYTE_ORDER_BIG_ENDIAN return in;#endif // !BYTE_ORDER_BIG_ENDIAN } // Convert little endian to a 64-bit native float static inline double LittleEndianToR64(double in) {#ifdef BYTE_ORDER_BIG_ENDIAN return ReverseEndian(in);#else // BYTE_ORDER_BIG_ENDIAN return in;#endif // !BYTE_ORDER_BIG_ENDIAN } // Switch the endian of an unsigned integer static inline unsigned short ReverseEndian(unsigned short in) { unsigned short out; ((unsigned char*)&out)[0] = ((unsigned char*)&in)[1]; ((unsigned char*)&out)[1] = ((unsigned char*)&in)[0]; return out; } // Switch the endian of an unsigned integer static inline short ReverseEndian(short in) { short out; ((unsigned char*)&out)[0] = ((unsigned char*)&in)[1]; ((unsigned char*)&out)[1] = ((unsigned char*)&in)[0]; return out; } // Switch the endian of an unsigned integer static inline unsigned int ReverseEndian(unsigned int in) { unsigned int out; ((unsigned char*)&out)[0] = ((unsigned char*)&in)[3]; ((unsigned char*)&out)[1] = ((unsigned char*)&in)[2]; ((unsigned char*)&out)[2] = ((unsigned char*)&in)[1]; ((unsigned char*)&out)[3] = ((unsigned char*)&in)[0]; return out; } // Switch the endian of an integer static inline int ReverseEndian(int in) { int out; ((unsigned char*)&out)[0] = ((unsigned char*)&in)[3]; ((unsigned char*)&out)[1] = ((unsigned char*)&in)[2]; ((unsigned char*)&out)[2] = ((unsigned char*)&in)[1]; ((unsigned char*)&out)[3] = ((unsigned char*)&in)[0]; return out; } // Switch the endian of a float static inline float ReverseEndian(float in) { float out; ((unsigned char*)&out)[0] = ((unsigned char*)&in)[3]; ((unsigned char*)&out)[1] = ((unsigned char*)&in)[2]; ((unsigned char*)&out)[2] = ((unsigned char*)&in)[1]; ((unsigned char*)&out)[3] = ((unsigned char*)&in)[0]; return out; } // Switch the endian of a double static inline double ReverseEndian(double in) { double out; ((unsigned char*)&out)[0] = ((unsigned char*)&in)[7]; ((unsigned char*)&out)[1] = ((unsigned char*)&in)[6]; ((unsigned char*)&out)[2] = ((unsigned char*)&in)[5]; ((unsigned char*)&out)[3] = ((unsigned char*)&in)[4]; ((unsigned char*)&out)[4] = ((unsigned char*)&in)[3]; ((unsigned char*)&out)[5] = ((unsigned char*)&in)[2]; ((unsigned char*)&out)[6] = ((unsigned char*)&in)[1]; ((unsigned char*)&out)[7] = ((unsigned char*)&in)[0]; return out; }};#endif // __GBITS_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -