Demonstrates a usage of functions performing bit shifts of arbitrary sized byte vectors.
#include <stdio.h>
#include <stdlib.h>
void DisplayVector(const void* pVector, size_t cbVector)
{
unsigned char* V = (unsigned char*) pVector;
if (!cbVector)
for (size_t i = 1; i < cbVector; ++i)
}
void DisplayVectorAsBigEndianInteger(const void* pVector, size_t cbVector)
{
unsigned char* V = (unsigned char*) pVector;
for (size_t i = 0; i < cbVector; ++i)
}
void DisplayVectorAsLittleEndianInteger(const void* pVector, size_t cbVector)
{
unsigned char* V = (unsigned char*) pVector + cbVector;
while (cbVector--)
}
int main(int argc, char** argv)
{
unsigned char data[] = {0xFE, 0xFD, 0xFC, 0xFB, 0xFA, 0xF9, 0xF8, 0xF7, 0xF6, 0xF5, 0xF4, 0xF4, 0xF3};
unsigned char big_buf[sizeof(data) * 2];
unsigned char med_buf[sizeof(data) + 4];
unsigned char sml_buf[sizeof(data) - 4];
size_t shift = 68u;
DisplayVector(data, sizeof(data));
PrintfA(
"=====Little-Endian shift=====\n");
DisplayVectorAsLittleEndianInteger(data, sizeof(data));
PrintfA(
"Left shift by %u bits modulo (1 << %u): ", shift,
sizeof(big_buf) * CHAR_BIT);
memlsh_LE(big_buf,
sizeof(big_buf), data,
sizeof(data), shift);
DisplayVectorAsLittleEndianInteger(big_buf, sizeof(big_buf));
DisplayVector(big_buf, sizeof(big_buf));
PrintfA(
"Left shift by %u bits modulo (1 << %u): ", shift,
sizeof(med_buf) * CHAR_BIT);
memlsh_LE(med_buf,
sizeof(med_buf), data,
sizeof(data), shift);
DisplayVectorAsLittleEndianInteger(med_buf, sizeof(med_buf));
DisplayVector(med_buf, sizeof(med_buf));
PrintfA(
"Left shift by %u bits modulo (1 << %u): ", shift,
sizeof(sml_buf) * CHAR_BIT);
memlsh_LE(sml_buf,
sizeof(sml_buf), data,
sizeof(data), shift);
DisplayVectorAsLittleEndianInteger(sml_buf, sizeof(sml_buf));
DisplayVector(sml_buf, sizeof(sml_buf));
memset(big_buf, 0xFF, sizeof(big_buf));
memlsh_LE(big_buf,
sizeof(big_buf), data,
sizeof(data), shift);
DisplayVector(big_buf, sizeof(big_buf));
memset(big_buf, 0xFF, sizeof(big_buf));
DisplayVector(big_buf, sizeof(big_buf));
memcpy(big_buf, data, sizeof(data));
DisplayVector(big_buf, sizeof(big_buf));
PrintfA(
"Right shift by %u bits modulo (1 << %u): ", shift,
sizeof(med_buf) * CHAR_BIT);
memrsh_LE(med_buf,
sizeof(med_buf), data,
sizeof(data), shift);
DisplayVectorAsLittleEndianInteger(med_buf, sizeof(med_buf));
DisplayVector(med_buf, sizeof(med_buf));
PrintfA(
"Right shift by %u bits modulo (1 << %u): ", shift,
sizeof(sml_buf) * CHAR_BIT);
memrsh_LE(sml_buf,
sizeof(sml_buf), data,
sizeof(data), shift);
DisplayVectorAsLittleEndianInteger(sml_buf, sizeof(sml_buf));
DisplayVector(sml_buf, sizeof(sml_buf));
memset(big_buf, 0xFF, sizeof(big_buf));
memrsh_LE(big_buf,
sizeof(big_buf), data,
sizeof(data), shift);
DisplayVector(big_buf, sizeof(big_buf));
memset(big_buf, 0xFF, sizeof(big_buf));
DisplayVector(big_buf, sizeof(big_buf));
memcpy(big_buf, data, sizeof(data));
DisplayVector(big_buf, sizeof(big_buf));
PrintfA(
"=====Big-Endian shift=====\n");
DisplayVectorAsBigEndianInteger(data, sizeof(data));
PrintfA(
"Left shift by %u bits modulo (1 << %u): ", shift,
sizeof(big_buf) * CHAR_BIT);
memlsh_BE(big_buf,
sizeof(big_buf), data,
sizeof(data), shift);
DisplayVectorAsBigEndianInteger(big_buf, sizeof(big_buf));
DisplayVector(big_buf, sizeof(big_buf));
PrintfA(
"Left shift by %u bits modulo (1 << %u): ", shift,
sizeof(med_buf) * CHAR_BIT);
memlsh_BE(med_buf,
sizeof(med_buf), data,
sizeof(data), shift);
DisplayVectorAsBigEndianInteger(med_buf, sizeof(med_buf));
DisplayVector(med_buf, sizeof(med_buf));
PrintfA(
"Left shift by %u bits modulo (1 << %u): ", shift,
sizeof(sml_buf) * CHAR_BIT);
memlsh_BE(sml_buf,
sizeof(sml_buf), data,
sizeof(data), shift);
DisplayVectorAsBigEndianInteger(sml_buf, sizeof(sml_buf));
DisplayVector(sml_buf, sizeof(sml_buf));
memset(big_buf, 0xFF, sizeof(big_buf));
memlsh_BE(big_buf,
sizeof(big_buf), data,
sizeof(data), shift);
DisplayVector(big_buf, sizeof(big_buf));
memset(big_buf, 0xFF, sizeof(big_buf));
DisplayVector(big_buf, sizeof(big_buf));
memcpy(big_buf, data, sizeof(data));
DisplayVector(big_buf, sizeof(big_buf));
PrintfA(
"Right shift by %u bits modulo (1 << %u): ", shift,
sizeof(med_buf) * CHAR_BIT);
memrsh_BE(med_buf,
sizeof(med_buf), data,
sizeof(data), shift);
DisplayVectorAsBigEndianInteger(med_buf, sizeof(med_buf));
DisplayVector(med_buf, sizeof(med_buf));
PrintfA(
"Right shift by %u bits modulo (1 << %u): ", shift,
sizeof(sml_buf) * CHAR_BIT);
memrsh_BE(sml_buf,
sizeof(sml_buf), data,
sizeof(data), shift);
DisplayVectorAsBigEndianInteger(sml_buf, sizeof(sml_buf));
DisplayVector(sml_buf, sizeof(sml_buf));
memset(big_buf, 0xFF, sizeof(big_buf));
memrsh_BE(big_buf,
sizeof(big_buf), data,
sizeof(data), shift);
DisplayVector(big_buf, sizeof(big_buf));
memset(big_buf, 0xFF, sizeof(big_buf));
DisplayVector(big_buf, sizeof(big_buf));
memcpy(big_buf, data, sizeof(data));
DisplayVector(big_buf, sizeof(big_buf));
return 0;
}
The file provides interfaces manage memory.
An interface of a subsystem performing printf/wprintf functionality which is portable to Windows and ...
void memlsh_LE_inplace(void *pValue, std::size_t cbValue, std::size_t nShift) noexcept
Performs an in-place bit-shift of a long integer specified by a vector of bytes in the Little-Endian ...
void memrsh_LE_inplace(void *pValue, std::size_t cbValue, std::size_t nShift) noexcept
Performs an in-place bit-shift of a long integer specified by a vector of bytes in the Little-Endian ...
void memrsh_LE_copy(void *restrict pResult, const void *restrict pValue, std::size_t cbValue, std::size_t nShift) noexcept
Bit-shifts a long unsigned integer specified by a vector of bytes in the Little-Endian order to the r...
void memrsh_LE(void *pResult, std::size_t cbResult, const void *pValue, std::size_t cbValue, std::size_t nShift) noexcept
Performs a linear bit shift of data representing an unsigned arbitrary precision integer in the Littl...
void memlsh_LE_copy(void *restrict pResult, const void *restrict pValue, std::size_t cbValue, std::size_t nShift) noexcept
Bit-shifts a long integer specified by a vector of bytes in the Little-Endian order to the left and c...
void memlsh_BE_inplace(void *pValue, std::size_t cbValue, std::size_t nShift) noexcept
Performs an in-place bit-shift of a long integer specified by a vector of bytes in the Big-Endian ord...
void memlsh_LE(void *pResult, std::size_t cbResult, const void *pValue, std::size_t cbValue, std::size_t nShift) noexcept
Performs a linear bit shift of data representing an unsigned arbitrary precision integer in the Littl...
void memlsh_BE(void *pResult, std::size_t cbResult, const void *pValue, std::size_t cbValue, std::size_t nShift) noexcept
Performs a linear bit shift of data representing an unsigned arbitrary precision integer in the Big-E...
void memrsh_BE(void *pResult, std::size_t cbResult, const void *pValue, std::size_t cbValue, std::size_t nShift) noexcept
Performs a linear bit shift of data representing an unsigned arbitrary precision integer in the Big-E...
void memlsh_BE_copy(void *restrict pResult, const void *restrict pValue, std::size_t cbValue, std::size_t nShift) noexcept
Bit-shifts a long integer specified by a vector of bytes in the Big-Endian order to the left and copi...
int PrintfA(const char *restrict pszFormat,...) noexcept
Loads data from the locations, defined by a variable arguments, converts it to string equivalents and...