## L1VM - course 09

### logical operators, bit shifting

#### logical operators

Here I will show how to use the logical operators in Brackets. They work like in C or other languages.

The “||” or operator:

`````` ((((x a ==)(x b ==) ||) f =) f if)
``````

The if will be executed if “x” equals “a” or “x” equals “b”. If you want to do an if when at least two cases are true then you use the “&&” and operator:

``````((((x a ==)(y b ==) &&) f =) f if)
``````

Here the if will be executed if “x” equals “a” and “y” equals “b”. If only one of the cases is true it will not be executed!

You can also write:

``````{i = (x == a) && (y == b)}
(i if)
// code inside of if
``````

#### bit shifting

``````{b = (a <| shift)}
``````

Here the “a” variable is “shifted” “shift” times to the left. If “a” is “2” and “shift” is “1” then we will get the value “4”. The bit pattern is shifted “1” bit to the left so the number is increasing. If the “shift” is “5” then we will get “64”.

The “>|” right shift operator works the other way round. So the result is the bit pattern shifted to the right by “shift” bits. The value will decrease.

Here is a full example:

``````// math-test.l1com - Brackets - new math expression test
//
//
#include <intr.l1h>
(main func)
(set int64 1 zero 0)
(set int64 1 xd 23)
(set int64 1 yd 42)
(set int64 1 zd 0)
(set int64 1 zerod 0)
(set int64 1 i 0)
(set int64 1 f 0)
(set int64 1 a 2)
(set int64 1 b 0)
(set int64 1 shift 5)
(set string s messagestr "xd < yd and xd > 0")
// new math expression:
{i = (xd < yd) && (xd > zerod)}
print_i (i)
print_n
(i if)
print_s (messagestr)
print_n
(endif)
((((xd yd <)(xd zerod >) &&) f =) f if)
print_s (messagestr)
print_n
(endif)
// shift
{b = (a <| shift)}
print_i (b)
print_n
{b = (b >| shift)}
print_i (b)
print_n
(255 zero 0 0 intr0)
(funcend)
``````