chsvlib
chsv helper source code
chsvmem\memory_shifts.c

Demonstrates a usage of functions performing bit shifts of arbitrary sized byte vectors.

#include <stdio.h>
#include <stdlib.h>
#include "chsvmem.h"
#include "chsvprintf.h"
void DisplayVector(const void* pVector, size_t cbVector)
{
unsigned char* V = (unsigned char*) pVector;
if (!cbVector)
PrintfA("{}\n");
PrintfA("{%02X", V[0]);
for (size_t i = 1; i < cbVector; ++i)
PrintfA(", %02X", V[i]);
PrintfA("}\n");
}
void DisplayVectorAsBigEndianInteger(const void* pVector, size_t cbVector)
{
unsigned char* V = (unsigned char*) pVector;
PrintfA("0x");
for (size_t i = 0; i < cbVector; ++i)
PrintfA("%02X", V[i]);
PrintfA("\n");
}
void DisplayVectorAsLittleEndianInteger(const void* pVector, size_t cbVector)
{
unsigned char* V = (unsigned char*) pVector + cbVector;
PrintfA("0x");
while (cbVector--)
PrintfA("%02X", *--V);
PrintfA("\n");
}
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;
/*Source vector: {FE, FD, FC, FB, FA, F9, F8, F7, F6, F5, F4, F4, F3}*/
PrintfA("Source vector: ");
DisplayVector(data, sizeof(data));
PrintfA("=====Little-Endian shift=====\n");
/*Source value: 0xF3F4F4F5F6F7F8F9FAFBFCFDFE*/
PrintfA("Source value: ");
DisplayVectorAsLittleEndianInteger(data, sizeof(data));
/*
Left shift by 68 bits modulo (1 << 208): 0x000000000F3F4F4F5F6F7F8F9FAFBFCFDFE00000000000000000
--as vector: {00, 00, 00, 00, 00, 00, 00, 00, E0, DF, CF, BF, AF, 9F, 8F, 7F, 6F, 5F, 4F, 4F, 3F, 0F, 00, 00, 00, 00}
*/
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));
PrintfA("\t--as vector: ");
DisplayVector(big_buf, sizeof(big_buf));
/*
Left shift by 68 bits modulo (1 << 136): 0x6F7F8F9FAFBFCFDFE00000000000000000
--as vector: {00, 00, 00, 00, 00, 00, 00, 00, E0, DF, CF, BF, AF, 9F, 8F, 7F, 6F}
*/
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));
PrintfA("\t--as vector: ");
DisplayVector(med_buf, sizeof(med_buf));
/*
Left shift by 68 bits modulo (1 << 72): 0xE00000000000000000
--as vector: {00, 00, 00, 00, 00, 00, 00, 00, E0}
*/
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));
PrintfA("\t--as vector: ");
DisplayVector(sml_buf, sizeof(sml_buf));
/*
memlsh_LE: {00, 00, 00, 00, 00, 00, 00, 00, E0, DF, CF, BF, AF, 9F, 8F, 7F, 6F, 5F, 4F, 4F, 3F, 0F, 00, 00, 00, 00}
memlsh_LE_copy: {00, 00, 00, 00, 00, 00, 00, 00, E0, DF, CF, BF, AF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF}
memlsh_LE_inplace: {00, 00, 00, 00, 00, 00, 00, 00, E0, DF, CF, BF, AF, 9F, 8F, 7F, 6F, 5F, 4F, 4F, 3F, FF, FF, FF, FF, FF}
*/
memset(big_buf, 0xFF, sizeof(big_buf));
PrintfA("memlsh_LE: ");
memlsh_LE(big_buf, sizeof(big_buf), data, sizeof(data), shift);
DisplayVector(big_buf, sizeof(big_buf));
memset(big_buf, 0xFF, sizeof(big_buf));
PrintfA("memlsh_LE_copy: ");
memlsh_LE_copy(big_buf, data, sizeof(data), shift);
DisplayVector(big_buf, sizeof(big_buf));
memcpy(big_buf, data, sizeof(data));
PrintfA("memlsh_LE_inplace: ");
memlsh_LE_inplace(big_buf, sizeof(big_buf), shift);
DisplayVector(big_buf, sizeof(big_buf));
/*
Right shift by 68 bits modulo (1 << 136): 0x0000000000000000000000000F3F4F4F5F
--as vector: {5F, 4F, 4F, 3F, 0F, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00}
*/
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));
PrintfA("\t--as vector: ");
DisplayVector(med_buf, sizeof(med_buf));
/*
Right shift by 68 bits modulo (1 << 72): 0x000000000F3F4F4F5F
--as vector: {5F, 4F, 4F, 3F, 0F, 00, 00, 00, 00}
*/
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));
PrintfA("\t--as vector: ");
DisplayVector(sml_buf, sizeof(sml_buf));
/*
memrsh_LE: {5F, 4F, 4F, 3F, 0F, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00}
memrsh_LE_copy: {5F, 4F, 4F, 3F, 0F, 00, 00, 00, 00, 00, 00, 00, 00, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF}
memrsh_LE_inplace: {5F, 4F, 4F, 3F, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, 0F, 00, 00, 00, 00, 00, 00, 00, 00}
*/
memset(big_buf, 0xFF, sizeof(big_buf));
PrintfA("memrsh_LE: ");
memrsh_LE(big_buf, sizeof(big_buf), data, sizeof(data), shift);
DisplayVector(big_buf, sizeof(big_buf));
memset(big_buf, 0xFF, sizeof(big_buf));
PrintfA("memrsh_LE_copy: ");
memrsh_LE_copy(big_buf, data, sizeof(data), shift);
DisplayVector(big_buf, sizeof(big_buf));
memcpy(big_buf, data, sizeof(data));
PrintfA("memrsh_LE_inplace: ");
memrsh_LE_inplace(big_buf, sizeof(big_buf), shift);
DisplayVector(big_buf, sizeof(big_buf));
PrintfA("=====Big-Endian shift=====\n");
/*Source value: 0xFEFDFCFBFAF9F8F7F6F5F4F4F3*/
PrintfA("Source value: ");
DisplayVectorAsBigEndianInteger(data, sizeof(data));
/*
Left shift by 68 bits modulo (1 << 208): 0x000000000FEFDFCFBFAF9F8F7F6F5F4F4F300000000000000000
--as vector: {00, 00, 00, 00, 0F, EF, DF, CF, BF, AF, 9F, 8F, 7F, 6F, 5F, 4F, 4F, 30, 00, 00, 00, 00, 00, 00, 00, 00}
*/
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));
PrintfA("\t--as vector: ");
DisplayVector(big_buf, sizeof(big_buf));
/*
Left shift by 68 bits modulo (1 << 136): 0xAF9F8F7F6F5F4F4F300000000000000000
--as vector: {AF, 9F, 8F, 7F, 6F, 5F, 4F, 4F, 30, 00, 00, 00, 00, 00, 00, 00, 00}
*/
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));
PrintfA("\t--as vector: ");
DisplayVector(med_buf, sizeof(med_buf));
/*
Left shift by 68 bits modulo (1 << 72): 0x300000000000000000
--as vector: {30, 00, 00, 00, 00, 00, 00, 00, 00}
*/
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));
PrintfA("\t--as vector: ");
DisplayVector(sml_buf, sizeof(sml_buf));
/*
memlsh_BE: {00, 00, 00, 00, 0F, EF, DF, CF, BF, AF, 9F, 8F, 7F, 6F, 5F, 4F, 4F, 30, 00, 00, 00, 00, 00, 00, 00, 00}
memlsh_BE_copy: {6F, 5F, 4F, 4F, 30, 00, 00, 00, 00, 00, 00, 00, 00, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF}
memlsh_BE_inplace: {6F, 5F, 4F, 4F, 3F, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, F0, 00, 00, 00, 00, 00, 00, 00, 00}
*/
memset(big_buf, 0xFF, sizeof(big_buf));
PrintfA("memlsh_BE: ");
memlsh_BE(big_buf, sizeof(big_buf), data, sizeof(data), shift);
DisplayVector(big_buf, sizeof(big_buf));
memset(big_buf, 0xFF, sizeof(big_buf));
PrintfA("memlsh_BE_copy: ");
memlsh_BE_copy(big_buf, data, sizeof(data), shift);
DisplayVector(big_buf, sizeof(big_buf));
memcpy(big_buf, data, sizeof(data));
PrintfA("memlsh_BE_inplace: ");
memlsh_BE_inplace(big_buf, sizeof(big_buf), shift);
DisplayVector(big_buf, sizeof(big_buf));
/*
Right shift by 68 bits modulo (1 << 136): 0x0000000000000000000000000FEFDFCFBF
--as vector: {00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0F, EF, DF, CF, BF}
*/
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));
PrintfA("\t--as vector: ");
DisplayVector(med_buf, sizeof(med_buf));
/*
Right shift by 68 bits modulo (1 << 72): 0x000000000FEFDFCFBF
--as vector: {00, 00, 00, 00, 0F, EF, DF, CF, BF}
*/
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));
PrintfA("\t--as vector: ");
DisplayVector(sml_buf, sizeof(sml_buf));
/*
memrsh_BE: {00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0F, EF, DF, CF, BF}
memrsh_BE_copy: {5F, 4F, 4F, 3F, 0F, 00, 00, 00, 00, 00, 00, 00, 00, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF}
memrsh_BE_inplace: {5F, 4F, 4F, 3F, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, 0F, 00, 00, 00, 00, 00, 00, 00, 00}
*/
memset(big_buf, 0xFF, sizeof(big_buf));
PrintfA("memrsh_BE: ");
memrsh_BE(big_buf, sizeof(big_buf), data, sizeof(data), shift);
DisplayVector(big_buf, sizeof(big_buf));
memset(big_buf, 0xFF, sizeof(big_buf));
PrintfA("memrsh_BE_copy: ");
memrsh_LE_copy(big_buf, data, sizeof(data), shift);
DisplayVector(big_buf, sizeof(big_buf));
memcpy(big_buf, data, sizeof(data));
PrintfA("memrsh_BE_inplace: ");
memrsh_LE_inplace(big_buf, sizeof(big_buf), shift);
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...