---
title: Operators
description: 
published: 1
date: 2021-12-14T12:22:23.317Z
tags: 
editor: markdown
dateCreated: 2020-12-17T18:53:07.008Z
---

# OPERATORS

| ![][menu] | [![][b1]][d1]&nbsp;&nbsp;&nbsp; [![][b2]][d2]&nbsp;&nbsp;&nbsp; [![][b3]][d3]&nbsp;&nbsp;&nbsp; [![][b4]][d4] &nbsp;&nbsp;&nbsp; [![][b5]][d5] <br> [![][b6]][d6]&nbsp;&nbsp;&nbsp; [![][b7]][d7]&nbsp;&nbsp;&nbsp; [![][b8]][d8] &nbsp;&nbsp;&nbsp;  [![][b9]][d9]&nbsp;&nbsp;&nbsp; [![][b10]][d10] <br> [![][b11]][d11]&nbsp;&nbsp;&nbsp; [![][b12]][d12] &nbsp;&nbsp;&nbsp;  [![][b13]][d13]&nbsp;&nbsp;&nbsp; [![][b14]][d14]&nbsp;&nbsp;&nbsp; [![][b15]][d15] <br> [![][b16]][d16]&nbsp;&nbsp;&nbsp;  [![][b17]][d17]&nbsp;&nbsp;&nbsp; [![][b18]][d18]&nbsp;&nbsp;&nbsp; [![][b19]][d19]&nbsp;&nbsp;&nbsp; [![][b20]][d20] <br> [![][b21]][d21]&nbsp;&nbsp;&nbsp; [![][b22]][d22]&nbsp;&nbsp;&nbsp; [![][b23]][d23]&nbsp;&nbsp;&nbsp; [![][b24]][d24]&nbsp;&nbsp;&nbsp;  [![][b25]][d25]  &nbsp;&nbsp;&nbsp;[![][b26]][d26]&nbsp;&nbsp;&nbsp; [![][b27]][d27]|
|---|:---|

&nbsp;&nbsp;&nbsp;    <br>


<!--- (COMMENT - INPUT Category Images (Used above)
-->
[spacer]: /reference_manual/input/spacer.png
[menu]: /reference_manual/menus/menu_operators_70p.png
[b1]: /reference_manual/operators/add_50p.png
[b2]: /reference_manual/operators/sub_50p.png
[b3]: /reference_manual/operators/mult_50p.png
[b4]: /reference_manual/operators/div_50p.png
[b5]: /reference_manual/operators/mod_50p.png
[b6]: /reference_manual/operators/abs_50p.png
[b7]: /reference_manual/operators/min_50p.png
[b8]: /reference_manual/operators/max_50p.png
[b9]: /reference_manual/operators/rand_50p.png
[b10]: /reference_manual/operators/less_50p.png
[b11]: /reference_manual/operators/lessequal_50p.png
[b12]: /reference_manual/operators/equal_50p.png
[b13]: /reference_manual/operators/unequal_50p.png
[b14]: /reference_manual/operators/greaterequal_50p.png
[b15]: /reference_manual/operators/greater_50p.png
[b16]: /reference_manual/operators/bool_tf_50p.png
[b17]: /reference_manual/operators/bool_not_50p.png
[b18]: /reference_manual/operators/bool_and_50p.png
[b19]: /reference_manual/operators/bool_or_50p.png
[b20]: /reference_manual/operators/istype_50p.png
[b21]: /reference_manual/operators/hex_50p.png
[b22]: /reference_manual/operators/and_50p.png
[b23]: /reference_manual/operators/or_50p.png
[b24]: /reference_manual/operators/xor_50p.png
[b25]: /reference_manual/operators/not_50p.png
[b26]: /reference_manual/operators/lshift_50p.png
[b27]: /reference_manual/operators/rshift_50p.png

<!--- (COMMENT - INPUT Category Detail Bookmarks (Used above)
-->
[d1]: /reference_manual/operators#
[d2]: /reference_manual/operators#
[d3]: /reference_manual/operators#
[d4]: /reference_manual/operators#
[d5]: /reference_manual/operators#modulus
[d6]: /reference_manual/operators#
[d7]: /reference_manual/operators#
[d8]: /reference_manual/operators#
[d9]: /reference_manual/operators#random
[d10]: /reference_manual/operators#
[d11]: /reference_manual/operators#
[d12]: /reference_manual/operators#
[d13]: /reference_manual/operators#
[d14]: /reference_manual/operators#
[d15]: /reference_manual/operators#
[d16]: /reference_manual/operators#boolean-t/f
[d17]: /reference_manual/operators#boolean-not
[d18]: /reference_manual/operators#boolean-and
[d19]: /reference_manual/operators#boolean-or
[d20]: /reference_manual/operators#boolean-is-type
[d21]: /reference_manual/operators#hex
[d22]: /reference_manual/operators#bitwise-and
[d23]: /reference_manual/operators#bitwise-or
[d24]: /reference_manual/operators#bitwise-xor
[d25]: /reference_manual/operators#bitwise-not
[d26]: /reference_manual/operators#bitwise-left-shift
[d27]: /reference_manual/operators#bitwise-right-shift



## <br>modulus
## Tab {.tabset}
### Description
![](/reference_manual/operators/mod.png)
The modulus (or "modulo" or "mod") is the remainder after dividing one number by another. e.g.: 10 mod 3 equals 1. Because 10/3 = 3 with a remainder of 1.

### Example
The variable **num** is assigned a random number 1-10. The MOD operation is used to probe the odd/even status of the number. If the remainder of the operation **num** divided by 2 results in 0, the number is even, else it is odd.

![](/reference_manual/operators/code_mod.png)

[Download Example](/reference_manual/examples/code_mod.ubp)


## <br>random
## Tab {.tabset}
### Description
![](/reference_manual/operators/rand.png)
Returns a different number between first and second values specified.

### Example
One of the common beginners' use of the RANDOM function is to program a HEADS or TAILS implementation. 

Here, two variables **heads** and **tails** are created, as well as a variable **num** to store the result of the RANDOM function.

In a loop of 1000 repetitions, heads and tails counts are tallied. Arbitrarily, 1 is counted as heads and 2 is counted as tails. The results are displayed at the end.

![](/reference_manual/operators/code_rand.png)

[Download Example](/reference_manual/examples/code_rand.ubp)


## <br>boolean t/f
## Tab {.tabset}
### Description
![](/reference_manual/operators/bool_tf.png)
Returns **true** or **false** depending on the position of the selector, or the **binary evaluation** of the expression used as input. 

You will see this block inside of many other blocks, where it is used to control the flow of the executions and events.

### Example
The IF block is a good example to demonstrate the true / false use of this block. The example code is executed two different ways:

First one is a FOREVER loop where the ELSE IF branch of the IF block is set to FALSE - meaning: do NOT evaluate this branch. Therefore, when run, only the first display message is shown. Even though the random block produces 2's, it does not get evaluated, because the else branch is turned off with the FALSE setting.

Also to note is the expression next to the IF. Originally, this position is the true / false block. It gets replaced by the expression **num = 1**. The **binary evaluation** of this expression is either **true** or **false** depending on what the generated number is. This demonstrates that expressions that evaluate to true or false can be used in place of this block.

![](/reference_manual/operators/code_bool_tf1only.png)

In the second one, the FOREVER loop is removed, and the ELSE branch is set to TRUE - meaning: evaluate this branch, if the IF branch is not true. So, if num is not 1 (meaning it is 2), this branch will execute and second message will be shown.

Since the random block generates 1's and 2's, the corresponding message is displayed depending on the number generated.

| ![](/reference_manual/operators/code_bool_tf1.png) | ![](/reference_manual/operators/code_bool_tf2.png)|
|--|--|

[Download Example](/reference_manual/examples/code_bool_tf.ubp)


## <br>boolean not
## Tab {.tabset}
### Description
![](/reference_manual/operators/bool_not.png)
Reverses the logical value associated with the expression on which it operates.
As shown, NOT will return FALSE if the slider is set to TRUE, and TRUE if the slider is set to FALSE.

It is also possible to substitute an expression that evaluates to a boolean true or false into the input slot. In that case, the block will evaluate the expression and depending on the result, return the opposite boolean value.

### Example
A variable called string is set to "MicroBlocks is great", and a variable called expression is set to "length of string = length of string".
The boolean evaluation of the expression is obviously TRUE.
When the boolean NOT is applied to the expression result, it is changed to the opposite: FALSE. All values are displayed.

![](/reference_manual/operators/code_bool_not.png)

[Download Example](/reference_manual/examples/code_bool_not.ubp)


## <br>boolean and
## Tab {.tabset}
### Description
![](/reference_manual/operators/bool_and.png)
Returns true only if either of its inputs are true and returns false otherwise.
It should be noted that since both conditions are required to be true for a true result, if a false is detected in the first condition, there is no need to evaluate the second one; false is returned immediately.

### Example
Two variables tuesday and raining are set to true.
Then we check to see if we need an umbrella by evaluating if it is Tuesday and it is raining. Both conditions have to be true for us to need an umbrella.
Play with settings and decidde yourself.

![](/reference_manual/operators/code_bool_and.png)

[Download Example](/reference_manual/examples/code_bool_and.ubp)


## <br>boolean or
## Tab {.tabset}
### Description
![](/reference_manual/operators/bool_or.png)
Returns true if either or both operands is true and returns false otherwise.

Similar to the boolean_AND but opposite of it, here only one of the two conditions have to be true for a true result. So if the first condition is true, there is no need to check the second one.

### Example
In this example we are randomly generating two numbers in the range of 1 - 5. Then we check to see if either one of the two numbers is equal to the number we are looking for. 

OR block is used to verify the result of the comparison. All values are reported.

![](/reference_manual/operators/code_bool_or.png)

[Download Example](/reference_manual/examples/code_bool_or.ubp)


## <br>is type
## Tab {.tabset}
### Description
![](/reference_manual/operators/istype.png)
Returns true if the input expession on the left side matches the data type menu selection made on the right side.

Data types are important when programming. While an environment like MicroBlocks makes it easy for the beginner users by taking care of any data type conversions, it is still possible to be fooled visually when comparing variables whose values look the same. See example provided.

### Example
Two variables data1 and data2 are set to similar looking values:

		data1 is set to number 2
    	data 2 is set to string "2".
      
IS TYPE block is used to verify the types of the variables. Even though they look the same, they are of DIFFERENT types.

Then we do arithmetic with both fields by adding 1 to each.

Since the data1 is already a number, no problems there.
data2 is a string, but has no visible alphabetic characters in front of the "2". 
Therefore, MicroBlocks is able to convert it to number 2 for us, and do the addition.

At the end, when we compare the values of data1+1 to data2+1, we get a TRUE because both of expressions evauate to number 3.

Now try the same after placing any letter into the first input field of the join block for data2, and see what results you will get.

![](/reference_manual/operators/code_istype.png)

[Download Example](/reference_manual/examples/code_istype.ubp)


## <br> Advanced
This section contains some blocks that represent **bitwise operations**. 

The bitwise operators are similar to the logical operators (booleans), except that they work on binary representations of data. 
Bitwise operators are used to change individual bits in an operand. 
Both operands associated with the bitwise operator must be integers.

To make it easier to follow the details and examples presented below, a reference table detailing Byte Positional values in decimal and hex, have been provided.


> **Byte Positions and Values**
>
> | Byte Position:|   7 |  6 |  5 |  4 |  3 |  2 |  1 |  0 |
> |---------------|----:|---:|---:|---:|---:|---:|---:|---:|
> | Decimal Value:| 128 | 64 | 32 | 16 |  8 |  4 |  2 |  1 |
> | Hex           |  80 | 40 | 20 | 10 | 08 | 04 | 02 | 01 |
> {.is-info}

<br>

### Anatomy of a **binary number:**
A binary number is a number expressed in the base-2 numeral system or binary numeral system, which uses only two symbols: typically "0" (zero) and "1" (one).

A **byte** is an eight bit binary number. Each **bit** is either a 0 or a 1.

To determine the decimal value of a binary number of eight bits, refer to the Byte Position table and add all decimal values for columns where the binary number has a 1.

**Example:**

<span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Binary Number: 0010 0110	&nbsp;&nbsp;&nbsp;	Decimal Value: 38</span>

      
**Explanation:**      
      
| Byte Position:|   7 |  6 |  5     |  4 |  3 |  2     |  1     |  0 |
|---------------|----:|---:|-------:|---:|---:|-------:|-------:|---:|
| Decimal Value:| 128 | 64 | **32** | 16 |  8 |  **4** |  **2** |  1 |
| Binary Number |  0  | 0  |  **1** |  0 |  0 |  **1** |  **1** |  0 | 


<span>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Total all the decimal values with a binary 1 in that column:
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Decimal Values: 32 + 4 + 2 = **38** 
</span>

## <br>hex
## Tab {.tabset}
### Description
![](/reference_manual/operators/hex.png)
Converts a hexadecimal string to a decimal number.

As a quick intro, hex numbers consist of 0 - 9 and A, B, C, D, E, F.
Thus, hex(3F) evalutes to 63 in decimal.

> Note that HEX block inputs have to be of type STRING, since part of the hex numbers consist of letters A -F.
{.is-warning}

hex to decimal conversion is a very common operation. Each hexadecimal digit represents exactly four bits, so one byte is represented by two digits. That simple mapping between digits and bytes makes hexadecimal useful when working with certain kinds of data. For example, RGB color codes.

>  **Dec - Hex - Bin Number Conversion**
> | Decimal| Hex | Binary |   |  Decimal | Hex |Binary |
> |-------:|----:|-------:|--:|---------:|----:|------:|
> |  **0** |  00 |  0000	|   |    **8** |  08 |  1000 |
> |  **1** |  01 |  0001  |   |    **9** |  09 |  1001 |
> |  **2** |  02 |  0010  |   |   **10** |  0A |  1010 |
> |  **3** |  03 |  0011  |   |   **11** |  0B |  1011 |
> |  **4** |  04 |  0100  |   |   **12** |  0C |  1100 |
> |  **5** |  05 |  0101  |   |   **13** |  0D |  1101 |
> |  **6** |  06 |  0110  |   |   **14** |  0E |  1110 |
> |  **7** |  07 |  0111  |   |   **15** |  0F |  1111 |
>
{.is-info}


### Example

|![](/reference_manual/operators/hex_cfdicon.png)||![](/reference_manual/operators/hex_cfd.png) |
|--|--|--|

Chrome browser's "Color For Developer's" extension enables one to pick a color from a suite of color palettes used on the web. The color is displayed in RGB format as three hex numbers. 

If we want to set the CLUE rgbLED color using the Neopixel Library blocks, we need to convert the RGB values into decimal equivalents. 

Example program accepts three hex numbers and displays their decimal equivalents. It then sets the NeoPixel RGB values accordingly.

|![](/reference_manual/operators/code_hex.png)||![](/reference_manual/common/adafruit-clue_350.png)|
|--|--|--|

[Download Example](/reference_manual/examples/code_hex.ubp)


## <br>bitwise and
## Tab {.tabset}
### Description
![](/reference_manual/operators/and.png)
The AND operator compares two bits and generates a result of 1 if both bits are 1; otherwise, it returns 0.

### Example
![](/reference_manual/operators/code_and.png)

**Explanation:**

| Byte Position:|   7 |  6 |  5 |  4 |  3 |  2 |  1 |  0 |
|---------------|----:|---:|---:|---:|---:|---:|---:|---:|
| Decimal Value:| 128 | 64 | 32 | 16 |  8 |  4 |  2 |  1 |
| 13            |  0  | 0  |  0 |  0 |  1 |  1 |  0 |  1 | 
| 7             |  0  | 0  |  0 |  0 |  0 |  1 |  1 |  1 | 
| Result = 5    |  0  | 0  |  0 |  0 |  0 |  1 |  0 |  1 | 

* Bitwise AND compares all the bit positions of both numbers and reports the ones where both are set to 1.
* Between #13 and #7, the positions where both numbers have a 1 are positions 2 and 0.
* The decimal value of position 2 is 4, and of position 0 is 1.
* Therefore the operation reports 4 + 1 = 5.


## <br>bitwise or
## Tab {.tabset}
### Description
![](/reference_manual/operators/or.png)
The OR operator compares two bits and returns 1 if either or both of the bits are 1 and it gives 0 if both bits are 0.

### Example
![](/reference_manual/operators/code_or.png)

**Explanation:**

| Byte Position:|   7 |  6 |  5 |  4 |  3 |  2 |  1 |  0 |
|---------------|----:|---:|---:|---:|---:|---:|---:|---:|
| Decimal Value:| 128 | 64 | 32 | 16 |  8 |  4 |  2 |  1 |
| 115           |  0  | 1  |  1 |  1 |  0 |  0 |  1 |  1 | 
| 9             |  0  | 0  |  0 |  0 |  1 |  0 |  0 |  1 | 
| Result = 123  |  0  | 1  |  1 |  1 |  1 |  0 |  1 |  1 | 

* Bitwise OR compares all the bit positions of both numbers and reports the ones where either are set to 1.
* Between #115 and #9, the  position where either numbers have a 1 are positions 6,5,4,3,1, and 0.
* The decimal value of position 2 is 4, and of position 0 is 1.
* Therefore the operation reports 64 + 32 + 16 + 8 + 2 + 1 = 123.


## <br>bitwise xor
## Tab {.tabset}
### Description
![](/reference_manual/operators/xor.png)
The EXCLUSIVE-OR operator compares two bits and generates a result of 1 if the bits are complementary (bit is set in one operand but not both); otherwise, it returns 0.

Another way to think of the XOR operation is that it will reverse the bit positions where the second operand has a 1. All others will be copied as the same.

### Example
![](/reference_manual/operators/code_xor.png)

**Explanation:**

| Byte Position:|   7 |  6 |  5 |  4 |  3 |  2 |  1 |  0 |
|---------------|----:|---:|---:|---:|---:|---:|---:|---:|
| Decimal Value:| 128 | 64 | 32 | 16 |  8 |  4 |  2 |  1 |
| 11            |  0  | 0  |  0 |  0 |  1 |  0 |  1 |  1 | 
| 12            |  0  | 0  |  0 |  0 |  1 |  1 |  0 |  0 | 
| Result = 7    |  0  | 0  |  0 |  0 |  0 |  1 |  1 |  1 | 

* Bitwise XOR compares all the bit positions of the first operand and reports the opposite of where the second operand has a 1.
* #12 has a 1 in positions 3 and 2. Bits in the same positions of #11 ( 10 ) will be reversed, all other bits of #11 will be copied as they are. For position 3 and 2 reversed, we get 01.
* We end up with 0000 0111 for the result. Positions 2, 1, and 0 are 1.
* The decimal value of position 2 is 4, position 1 is 2, and position 0 is 1.
* Therefore the operation reports 4 + 2 + 1 = 7.


## <br>bitwise not
## Tab {.tabset}
### Description
![](/reference_manual/operators/not.png)
The NOT (or COMPLEMENT) operator is used to invert (BIT FLIP) all of the bits of the operand.
When the highest position bit of a number becomes 1, that number is considered a negative number.

Actual computer representation of an signed integer in MicroBlocks is 31 bits long. The high order bit #31 is a sign bit. This allows numbers in the range of -1073741824 to 1073741823.

When the sign bit is 0 the number is positive; and when it is 1, the number is negative.

> NOT operation reverses all bits and turns positive numbers into negative ones and vice versa. 
{.is-info}


### Example
![](/reference_manual/operators/code_not.png)

**Explanation:**

NOT operation reverses all bits and turns positive numbers into negative ones. 

Decimal **3** ( Binary: **0011** ) becomes decimal **-4** ( Binary: **1100** ).

**NOTE:** In the 4-bit example used here, the high order bit (bit-3) is the sign bit. 



## <br>bitwise left shift
## Tab {.tabset}
### Description
![](/reference_manual/operators/lshift.png)
The left operands value is moved left by the number of bits specified by the right operand.

The bits are moved to the left, the far left bit is discarded, and the rightmost bit is assigned a value of 0. 
For every bit position moved to the left, this operation is the binary equivalent of multiplying by 2.

> **REMEMBER**
> For signed positive numbers that naturally have a 0 bit in the most significant position of their binary representation, this operation will eventually turn that into a 1 bit, thus changing the number to a negative one.
{.is-warning}


### Example
![](/reference_manual/operators/code_lshift.png)

Decimal number 7 is Left shifted 1 bit position. 
Bit positions of the result are: positions 3, 2, and 1; with corresponding values of 8, 4, 2.
Therefore the operation reports: 8 + 4 + 2 = 14.

**Explanation:**

| Byte Position:|   7 |  6 |  5 |  4 |  3 |  2 |  1 |  0 |
|:-------------:|----:|---:|---:|---:|---:|---:|---:|---:|
| Decimal Value:| 128 | 64 | 32 | 16 |  8 |  4 |  2 |  1 |
|  7            |  0  | 0  |  0 |  0 |  0 |  1 |  1 |  1 | 
| 14            |  0  | 0  |  0 |  0 |  1 |  1 |  1 |  0 | 


## <br>bitwise right shift
## Tab {.tabset}
### Description
![](/reference_manual/operators/rshift.png)
The left operands value is moved right by the number of bits specified by the right operand.

The bits are moved to the right, the far right bit is discarded, and the leftmost bit is assigned a copy of the sign bit. This is called an **arithmetic shift**.
For every bit position moved to the right, this operation is the binary equivalent of dividing by 2.

> **REMEMBER**
> For signed negative numbers that naturally have a 1 bit in the most significant position of their binary representation, this operation will maintain the sign bit and successively produce negative numbers.
{.is-warning}

### Example
![](/reference_manual/operators/code_rshift.png)

Decimal number 7 is Left shifted 1 bit position. 
Bit positions of the result are: positions 4, 3, 1, and 0; with corresponding values of 16, 8, 2, and 1.
Therefore the operation reports: 16 + 8 + 2 + 1 = 27.

**Explanation:**

| Byte Position:|   7 |  6 |  5 |  4 |  3 |  2 |  1 |  0 |
|:--------------|----:|---:|---:|---:|---:|---:|---:|---:|
| Decimal Value:| 128 | 64 | 32 | 16 |  8 |  4 |  2 |  1 |
| 54            |  0  | 0  |  1 |  1 |  0 |  1 |  1 |  0 | 
| 27            |  0  | 0  |  0 |  1 |  1 |  0 |  1 |  1 | 

