Search code examples
bit-manipulationcapl

I am setting a bunch of Bits into a variable position in a byte and it won't work correctly


I am doing this in CAPL (very similar to C):

data[i] = (data[i] & (~mstart)) + (z_Checksum_ldu8[i] & mstart);
data[i] has the value 0x20 and z_Checksum_ldu8[i] is 0x0C
mstart is 0x0F
desired result is 0x2C
actual result is 0x0C

while data[ ] and y_Checksum_ldu8[ ] are of the type "byte" and mstart is also a byte (used it as a bitmask). The rest of the Code is not important, because I can see the result in the debugger. And what I see is not what I want!


First of all, why am I doing this?

  • I want to set the lsb-nibble exactly to the values of the checksum variable lsb-nibble
  • The msb-nibble of data[i] should be the same after the operation ends

But it won't and this is my question: Where is my fault, that the msb-nibble gets set to 0x0 everytime? It's 100% sure, the error must be in that line, cause I can see the debugger value changing to 0x0X (X for checksum) directly after executing this line of code.


Solution

  • Although both types are unsigned, it only works out using | not + .