cpct_set6Bits

Sets the value of a selected element of 6 bits into a bitarray to [0-63].

C Definition

u8 cpct_set6Bits (void* array, u16 value, u16 index);

Input Parameters (6 Bytes)

(2B DE) arrayPointer to the first byte of the array
(2B BC) valueNew value [0-63] for the element of 6 bits at the given position.  If you call from assembly, you can safely ignore B register and set only C register.
(2B HL) indexIndex of the group of 6 bits in the array to be modified

Assembly call (Input parameters on registers)

call cpct_set6Bits_asm

Parameter Restrictions

  • array must be the memory location of the first byte of the array.  However, this function will accept any given 16-value, without performing any check.  So, be warned that giving mistaken values to this function will produce unexpected behaviour (it may change random bits from memory).
  • value new value for the selected 6-bits element [0-63].  Be sure not to use values greater than 63 or lower than 0.  Wrong values can potentially change random bits from values near the selected one.
  • index position of the 6-bits element to be set in the array, starting in 0.  As this function does not perform any boundary check, if you gave an index outside the boundaries of the array, this function will overwrite a random location in memory, yielding unexpected results.

Known limitations

  • Maximum of 65536 elements of 6-bits, 49152 bytes per array.

Details

Set the new value of the 6-bits element at the given position (index) in the specified array.  This function calculates the location in memory considering each value to take 6-bits in memory.  Then, the 6 Least Significant Bits of value are inserted.  However, take into account that values greater than 63 or lower than 0 (that is, requiring more than 6 bits) could potentially modify bits from nearby elements on insertion.

Examples

u8 i;

// Declare and initialize a 6-bit Array using some useful macros
const CPCT_6BITARRAY(my_array, 8) = {
   CPCT_ENCODE6BITS(10, 12, 31, 45),
   CPCT_ENCODE6BITS( 7, 60, 18,  2)
};

// Multiply elements lower than 10 by 2
for(i=0; i < 8; i++) {
   u8 value = cpct_get6Bits(my_array, i);
   cpct_set6Bits(my_array, value*2, i);
}

// Show all the values. This should print: 10 12 31 45 14 60 18 4
//
for(i=0; i < 8; i++) {
   printf("%d ", cpct_get6Bits(my_array, i));
}

Destroyed Register values

AF, BC, DE, HL

Required memory

C-bindings94 bytes
ASM-bindings89 bytes

Time Measures

Case      | microSecs (us) | CPU Cycles |
-----------------------------------------
Best (0)  |      66        |     264    |
-----------------------------------------
Worst(2)  |      81        |     324    |
-----------------------------------------
ASM Saving|     -15        |     -60    |
-----------------------------------------

We first divide INDEX / 4 because each 4 values are stored in groups of 3-bytes.  Therefore, this lets us count how many groups of 3-bytes do we have to advance to get to the 3-bytes where the target value to be changed is.  The remainder of the division tells us which one of the 4 values contained in that target 3-byte group, contains the target value.  Each group of 3-bytes stores the 4 values as follows: [00000011][11112222][22333333] New number to insert contained in BC is assumed to be [00000000][00xxxxxx]

unsigned char (u8 = unsigned 8-bits, 1 byte )
Sets the value of a selected element of 6 bits into a bitarray to [0-63].
unsigned int (u16 = unsigned 16-bits, 2 bytes)
Close