Sets the value of a selected element of 6 bits into a bitarray to [0-63].
u8 cpct_set6Bits (void* array, u16 value, u16 index);
(2B DE) array | Pointer to the first byte of the array |
(2B BC) value | New 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) index | Index of the group of 6 bits in the array to be modified |
call cpct_set6Bits_asm
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.
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)); }
AF, BC, DE, HL
C-bindings | 94 bytes |
ASM-bindings | 89 bytes |
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]