diff options
| author | Jef <jef@targetspot.com> | 2024-09-24 08:54:57 -0400 |
|---|---|---|
| committer | Jef <jef@targetspot.com> | 2024-09-24 08:54:57 -0400 |
| commit | 20d28e80a5c861a9d5f449ea911ab75b4f37ad0d (patch) | |
| tree | 12f17f78986871dd2cfb0a56e5e93b545c1ae0d0 /Src/WAT/wa_bits_operation.cpp | |
| parent | 537bcbc86291b32fc04ae4133ce4d7cac8ebe9a7 (diff) | |
| download | winamp-20d28e80a5c861a9d5f449ea911ab75b4f37ad0d.tar.gz | |
Initial community commit
Diffstat (limited to 'Src/WAT/wa_bits_operation.cpp')
| -rw-r--r-- | Src/WAT/wa_bits_operation.cpp | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/Src/WAT/wa_bits_operation.cpp b/Src/WAT/wa_bits_operation.cpp new file mode 100644 index 00000000..9688cfc6 --- /dev/null +++ b/Src/WAT/wa_bits_operation.cpp @@ -0,0 +1,38 @@ +#include "WAT.h" + + +unsigned char* wa::bits_operation::GetBits(unsigned char* Source, unsigned int NbrOfBits, unsigned int* BufferSize) +{ + // check for wrong parameter + if (Source == nullptr || NbrOfBits == 0 || BufferSize == nullptr) + return nullptr; + + // variable + unsigned int bitMask = 0; + unsigned int nbrOfByteToRead = 1 + (NbrOfBits-1) / 8; + unsigned char* bufferToReturn = (unsigned char*)malloc(nbrOfByteToRead); + memset(bufferToReturn, 0, nbrOfByteToRead); + *BufferSize = nbrOfByteToRead; + // copy all bytes + if (nbrOfByteToRead > 1) + { + memcpy(bufferToReturn, Source, nbrOfByteToRead - 1); + } + // copy the specific end bits + bitMask = (1 << NbrOfBits - ((nbrOfByteToRead - 1)*8)) - 1; + bufferToReturn[nbrOfByteToRead - 1] = Source[nbrOfByteToRead - 1] & bitMask; + return bufferToReturn; +} + +wa::strings::wa_string wa::bits_operation::PrintInBinary(unsigned char* buffer, unsigned int size) +{ + wa::strings::wa_string ToReturn = ""; + for (unsigned int NbrOfByte = 0; NbrOfByte < size; ++NbrOfByte) + { + for (int IndexBit = 0; IndexBit < 8; ++IndexBit) + ToReturn.append((buffer[NbrOfByte] & (1 << IndexBit)) ? "1" : "0"); + ToReturn.append(" ' "); + } + return ToReturn; + +} |
