Livt packages
Eccelerators.BitTwiddling
Version 0.1.1
Readable 32-bit bit manipulation, counting, scanning, permutation, and arithmetic utilities for Livt.
Published files (26)
src/bytes/ByteOperations.lvt
namespace Eccelerators.BitTwiddling.Bytes
/**
* Provides indexed access to the four bytes of an unsigned 32-bit word.
*
* Byte index 0 selects bits 7 through 0 and byte index 3 selects bits 31
* through 24. This explicit little-endian index convention is independent of
* host memory layout because the operations work on values, not pointers.
*/
class ByteOperations
{
/**
* Returns the byte at index from value.
*
* Valid indices are 0 through 3. An invalid index returns 0x00.
*/
public static fn GetByte[](value: uint, index: int) byte
{
if ((index < 0) || (index > 3))
{
return 0x00
}
var distance: int = index * 8
return ((value >> distance) & (0xFF as uint)) as byte
}
/**
* Replaces the byte at index with byteValue and returns the updated word.
*
* Valid indices are 0 through 3. An invalid index returns value unchanged.
*/
public static fn SetByte[](value: uint, index: int, byteValue: byte) uint
{
if ((index < 0) || (index > 3))
{
return value
}
var distance: int = index * 8
var mask: uint = (0xFF as uint) << distance
var positionedValue: uint = (byteValue as uint) << distance
return (value & ~mask) | positionedValue
}
/**
* Exchanges the two bytes in the low 16 bits of value.
*
* Bits 31 through 16 are discarded and are zero in the returned uint.
*/
public static fn ByteSwap16[](value: uint) uint
{
var lowWord: uint = value & (0xFFFF as uint)
return ((lowWord & (0x00FF as uint)) << 8) |
((lowWord & (0xFF00 as uint)) >> 8)
}
}