8.9.7 builtin_avr_insert_bits Built-in Function

Inserts bits into val in a manner dictated by a mapping value and returns the resulting value.

Prototype

uint8_t __builtin_avr_insert_bits (uint32_t map, uint8_t bits, uint8_t val)

Remarks

Insert bits from bits into val in a manner dictated by map, and return the resulting value.

Each of the 8 nibbles (4 bits) of map controls a bit in the returned value whose bit position is the same as the nibble's nibble position in map. For example, the most-significant nibble of map controls insertion into the most-significant bit of the returned value, etc. If a nibble in map is 0xF, then the corresponding bit in the returned value is the corresponding bit of val, unmodified. Nibbles with a value of 0 through 7 indicate that the corresponding bit in the returned value is obtained from the bit with bit position within bits.

Example

#include <stdint.h>
#include <stdio.h>

int main(void) {    
  uint8_t  result, myValue, myBits;
  volatile char dummy;

  myValue = 0xF6;
  myBits  = 0x3A;
    
  result = __builtin_avr_insert_bits(0x01234567, myBits, myValue);
  printf("result is 0x%X\n", result);
  result = __builtin_avr_insert_bits(0x5566FFFF, myBits, myValue);
  printf("result is 0x%X\n", result);
}

Example Output

result is 0x5C
result is 0xC6

Example Explanation

In the first usage of the built-in in the above example, the first nibble in the map value (0x0) indicates that the most-significant bit of the result will be derived from bit #0 in myBits. The next lower-significant bit will be derived from bit #1, etc. Given the map nibbles in the above example, the first execution of the built-in will return a value being that of myBits with its bits appearing in reverse order.

In the second usage of the built-in, the lower four nibble of map are 0xF, which implies that the lower four bits of the result value will be the lower four bits of myValue, unmodified. The two most-significant nibbles of map are 0x5, indicating that the two most-significant bits of the returned value will be bit #5 of myBits (which is 1). Similarly, the next two bits of the result will be bit #6 of myBits (which is 0).