Universal Robots Forum

Bit operation in urscript

How to do bit operation in URScript ?

I have integer variables and would like to do bitmasking, and/or set/get bit status, similar to get_flag() / set_flag() but with integer variables.
In C I would use: a = b & mask; b = c | 64;
And later I would use it in conditional statement: IF (b & 64) …

You can use these functions:

variable_as_int = (read value here)
# variable_as_int may be 11
variable_as_bool_list = integer_to_binary_list(variable_as_int)
# variable_as_bool_list is then [True, True, False, True, False, ...] = 0b00001011
# Length is 32, LSB as position 0

# Now overwrite to [True, False, False, True, False, ...] = integer 9 = 0b00001001
variable_as_bool_list[1] = False

# Check value of e.g. 5th position
if (variable_as_bool_list[5] == True):
    # do something
end

# Convert back to integer
variable_as_int = binary_list_to_integer(variable_as_bool_list)

The functions integer_to_binary_list(int) and binary_list_to_integer(bool_list) are explained in the URScript Manual.

thank you for your information.
It might be good to have bit operators to simplify script.