Skip to content

Commit

Permalink
Added function ExtractTwoBits
Browse files Browse the repository at this point in the history
  • Loading branch information
dc42 committed Jul 26, 2022
1 parent 5c65648 commit 0db112d
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/General/Bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ template<class T> inline constexpr T ExtractBit(T val, unsigned int fromBitNumbe
}
}

// Extract two bits from a value and move it to a target bit number, returning a value with only the target bits possibly set
// T should be an unsigned integer type
template<class T> inline constexpr T ExtractTwoBits(T val, unsigned int fromBitNumber, unsigned int toBitNumber) noexcept
{
if (fromBitNumber == toBitNumber)
{
return val & ((T)3 << toBitNumber);
}
else if (toBitNumber > fromBitNumber)
{
return (val << (toBitNumber - fromBitNumber)) & ((T)3 << toBitNumber);
}
else
{
return (val >> (fromBitNumber - toBitNumber)) & ((T)3 << toBitNumber);
}
}

template<class BaseType> class Bitmap
{
public:
Expand Down

0 comments on commit 0db112d

Please sign in to comment.