# Bit operation in urscript

**URL:** <https://forum.universal-robots.com/t/bit-operation-in-urscript/1471>\
**Category:** URScript\
**Created:** [January 7, 2018, 12:45am UTC](https://forum.universal-robots.com/t/bit-operation-in-urscript/1471 "2018-01-07T00:45:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon28553715](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon28553715](https://forum.universal-robots.com/u/anon28553715)\
**Post date:** [January 7, 2018, 12:45am UTC](https://forum.universal-robots.com/t/bit-operation-in-urscript/1471/1 "2018-01-07T00:45:15Z")

</div>

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) …

---

<div class="post-metadata">

**Author:** ![jbm](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/jbm/32/614_2.png) [@jbm](https://forum.universal-robots.com/u/jbm)\
**Post date:** [January 8, 2018, 7:54am UTC](https://forum.universal-robots.com/t/bit-operation-in-urscript/1471/2 "2018-01-08T07:54:47Z")

</div>

You can use these functions:

```auto
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](https://www.universal-robots.com/download/?option=32553#section32324).

---

<div class="post-metadata">

**Author:** ![anon28553715](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon28553715](https://forum.universal-robots.com/u/anon28553715)\
**Post date:** [January 9, 2018, 3:29am UTC](https://forum.universal-robots.com/t/bit-operation-in-urscript/1471/3 "2018-01-09T03:29:59Z")

</div>

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