transformUint32Bits function

int transformUint32Bits(
  1. int uint32,
  2. int shift,
  3. int bitmask,
  4. int newValue
)

Replaces specific bits in a 32-bit unsigned integer.

Bits are specified by:

  • shift - Bitmask left-shift (0,1, ..., 30, 31)
  • bitmask - For example, 0xF for 4 bits.

Implementation

int transformUint32Bits(int uint32, int shift, int bitmask, int newValue) {
  if (bitmask | newValue != bitmask) {
    throw new ArgumentError.value(newValue, "newValue", "too many bits");
  }
  return ((0xFFFFFFFF ^ (bitmask << shift)) & uint32) | (newValue << shift);
}