Last active
December 18, 2016 13:18
-
-
Save lukicdarkoo/9e35bf08e0c12205a1f1f0a280ed8413 to your computer and use it in GitHub Desktop.
Bits manipulation module for fast prototyping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Bits.h" | |
void Bits_ByteDump(uint64_t value, uint8_t numberOfBytes) { | |
int8_t i; | |
for (i = numberOfBytes * 8 - 1; i >= 0; i--) { | |
printf("%ld", (value >> i) & 1); | |
if (i % 8 == 0) { | |
printf(" "); | |
} | |
} | |
printf("\n"); | |
} | |
uint8_t Bits_ByteSlice(uint8_t byte, uint8_t start, uint8_t length) { | |
uint8_t mask = (1 << length) - 1; | |
return (byte >> (8 - start - length)) & mask; | |
} | |
uint64_t Bits_BufferSlice(const uint8_t* const buffer, uint16_t start, uint8_t length) { | |
uint64_t result = 0; | |
uint8_t byteIndexStart = start / 8; | |
uint8_t bitIndexStart = start % 8; | |
int8_t bitsToRead = length; | |
while (bitsToRead > 0) { | |
uint8_t currentBitsToRead = (bitsToRead < (8 - bitIndexStart)) ? bitsToRead : (8 - bitIndexStart); | |
bitsToRead -= currentBitsToRead; | |
result |= (Bits_ByteSlice(buffer[byteIndexStart], bitIndexStart, currentBitsToRead) << bitsToRead); | |
byteIndexStart++; | |
bitIndexStart = 0; | |
} | |
return result; | |
} | |
uint8_t Bits_ByteSet(uint8_t value, uint8_t position) { | |
return (value | (1 << position)); | |
} | |
uint8_t Bits_ByteClear(uint8_t value, uint8_t position) { | |
return ~(1 << position) & value; | |
} | |
uint8_t Bits_ByteGet(uint8_t value, uint8_t position) { | |
return ((1 << position) & value) >> position; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _BITS_H_ | |
#define _BITS_H_ | |
/********************************************* | |
* @brief Module enables easy byte manipulation | |
* @author Darko Lukic <[email protected]> | |
* @hint Function name pattern: Bits_[Byte|Buffer][Operation] | |
* | |
**********************************************/ | |
#include <stdio.h> | |
#include <inttypes.h> | |
/********************************************** | |
* @brief Print bits | |
* | |
* @param [in] value - value to print | |
* @param [in] numberOfBytes - size of value in bytes | |
* | |
**********************************************/ | |
void Bits_ByteDump(uint64_t value, uint8_t numberOfBytes); | |
/********************************************** | |
* @brief Slice bits from byte | |
* @param [in] byte - Byte | |
* @param [in] start - Index of start bit | |
* @param [in] length - Number of bits to slice | |
* | |
* @return Sliced bits | |
* | |
**********************************************/ | |
uint8_t Bits_ByteSlice(uint8_t byte, uint8_t start, uint8_t length); | |
/********************************************** | |
* @brief Slice bits from buffer | |
* @param [in] Buffer - Buffer | |
* @param [in] start - Index of start bit | |
* @param [in] length - Number of bits to slice | |
* | |
* @return Sliced bits | |
* | |
**********************************************/ | |
uint64_t Bits_BufferSlice(const uint8_t* const buffer, uint16_t start, uint8_t length); | |
uint8_t Bits_ByteSet(uint8_t value, uint8_t position); | |
uint8_t Bits_ByteClear(uint8_t value, uint8_t position); | |
uint8_t Bits_ByteGet(uint8_t value, uint8_t position); | |
#endif // _BITS_H_ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment