stuff-from-scratch/src/base/core/encoding/Bits.h
2024-01-02 16:14:23 +00:00

67 lines
1.8 KiB
C++

#pragma once
#include "ByteTypes.h"
#include "String.h"
class Bits
{
public:
static bool MostSignificantBitIsOne(Byte c);
static Word GetWordFirstBit(const Word word);
static Word GetWordLastByte(const Word word);
static Byte getByteN(DWord input, size_t n);
static Byte getHigherNBits(Byte input, size_t num);
static Byte getLowerNBits(DWord input, size_t num);
static Byte getTwoBitsAtN(Byte input, size_t n);
static Byte getMBitsAtN(Byte input, size_t m, size_t n);
static bool getBitN(DWord input, size_t n);
static Byte getFromString(const String& string);
static String toString(DWord input, size_t length = 8);
static DWord mirror(DWord input, size_t length=0);
static void ReverseBuffer(Byte* buffer, char* reverse, size_t size, size_t targetSize);
template<typename T>
static T ToType(Byte* buffer, bool reverse = true)
{
T result {0};
if(reverse)
{
char reversed[sizeof(T)];
ReverseBuffer(buffer, reversed, sizeof(T), sizeof(T));
//std::memcpy(&result, reversed, sizeof(T));
}
else
{
//std::memcpy(&result, buffer, sizeof(T));
}
return result;
}
static Word ToWord(Byte* buffer, bool reverse = true);
static DWord ToDWord(Byte* buffer, bool reverse = true);
static QWord ToQWord(Byte* buffer, bool reverse = true);
static bool Compare(Byte* buffer, const char* tag, size_t size);
static bool CompareDWords(Byte* buffer, DWord target);
static bool CompareWords(Byte* buffer, Word target);
static const int BYTE_FIRST_BIT = 0x40; // 1000 0000
static const Word WORD_FIRST_BIT = static_cast<Word>(0x8000); // 1000 0000 - 0000 0000
static const Word WORD_LAST_BYTE = 0x00FF; // 0000 0000 - 1111 1111
};