pythonbitwise-operatorscocotb

Python bitmask doesn't give expected output


Im puting a bitmask over a register read value and execute a logical and on the value. what i expect is: clear_reg_val: 00000000000000000100100000000000 My actual output is:

# reg_value is:  00000000000000000100100000001100
# mask_inverted: 11111111111111111111100000000011
# clear_reg_val: 00000000000000000100100000001000
# reg_value is:  00000000000000000100100000001100
# mask_inverted: 11111111111111111111100000000011
# clear_reg_val: 00000000000000000100100000001000

So i don't know why bit 4 of clear_reg_val becomes a '1' instead of a '0' The Python code of the function is added in the screenshot below:

Python function for read modify write


Solution

  • The error is in the way you are creating the inverted mask.

    You are doing it as a string operation, and converting the string back to an integer using int. You forgot to specify base=2 and the string is treated as a decimal number.

    The proper way to invert a 32 bit value is:

    MASK32 = 2**32-1
    
    invx = ~x & MASK32