Understanding Operators in Sheerpower
Operators are the symbols that perform operations on variables
and values. They are the building blocks for doing math, making
comparisons, and creating logic in your programs. This tutorial
is broken down into three parts to make learning easy and fun.
Part 1: The Basics - Mathematical Operators
These are the operators you use for basic arithmetic.
+ (Addition)
- (Subtraction)
* (Multiplication)
/ (Division)
^ (Exponentiation, e.g., 2^3 is 8)
++ (Increment, adds 1 to a variable, e.g., counter++)
Example: Calculating a Shopping Bill
apple_price = 0.50
banana_price = 0.30
num_apples = 4
num_bananas = 6
! Calculate the cost for each fruit
total_apple_cost = apple_price * num_apples
total_banana_cost = banana_price * num_bananas
! Calculate the subtotal
subtotal = total_apple_cost + total_banana_cost
print "Subtotal: $"; subtotal
! Add one more apple
num_apples++
print "Number of apples is now: "; num_apples
Part 2: Making Decisions - Relational and Logical Operators
These operators are used to compare values and combine conditions,
which is essential for making decisions in your code.
Relational Operators (for comparing values)
= (Equal to)
<> (Not equal to)
> (Greater than)
< (Less than)
>= (Greater than or equal to)
<= (Less than or equal to)
Logical Operators (for combining true/false conditions)
AND (Returns true if BOTH conditions are true)
OR (Returns true if AT LEAST ONE condition is true)
NOT (Reverses the condition, true becomes false)
Example: Qualifying for a Discount
is_member? = true
purchase_total = 75.00
! Check if the customer gets a discount
is_eligible? = is_member? OR purchase_total > 100.00
print "Is eligible for discount? "; is_eligible?
Logical Operators on Numeric Values (Bitwise Operations)
When used with numeric values, these same operators perform
bitwise operations. Each bit of the number is
processed individually.
AND (Bitwise AND — keeps bits that are set in BOTH values)
OR (Bitwise OR — keeps bits set in EITHER value)
NOT (Bitwise NOT — flips all bits)
XOR (Bitwise exclusive OR — keeps bits that differ)
Example: Working with Flags
// Example bit flags
read_flag = 1 // 0001
write_flag = 2 // 0010
execute_flag = 4 // 0100
// Combine flags using OR
permissions = read_flag OR write_flag // 0011
// Test if write flag is set
has_write? = (permissions AND write_flag) <> 0
// Toggle a flag using XOR
permissions = permissions XOR write_flag // removes write_flag
print "Permissions: "; permissions
print "Has write? "; has_write?
Bitwise vs Logical — Why It Matters
Problem: Developers often assume AND/OR always
operate on true/false values, which leads to confusion when numbers
are involved.
Solution: In Sheerpower, the same operators work
consistently across both boolean and numeric contexts. When applied
to numbers, they operate at the bit level.
Efficiency: Bitwise operations are extremely fast
and allow compact representation of multiple flags inside a single
numeric value.
Advanced Logical Operators
Sheerpower also supports more advanced logical operators. Their
behavior is easiest to understand using "Truth Tables."
Truth Table for XOR, IMP, and EQV
| X | Y |
X XOR Y |
X IMP Y |
X EQV Y |
| TRUE | TRUE |
FALSE |
TRUE |
TRUE |
| TRUE | FALSE |
TRUE |
FALSE |
FALSE |
| FALSE | TRUE |
TRUE |
TRUE |
FALSE |
| FALSE | FALSE |
FALSE |
TRUE |
TRUE |
Part 3: How It All Works - Order of Operations
Sheerpower, like all programming languages, follows a specific
order when evaluating expressions. This is called
operator precedence. Understanding this order is
crucial for getting the correct results.
Order of Operations
- Parentheses `()`
Expressions inside parentheses are always evaluated first.
- Exponentiation `^`
Powers are calculated next.
- Multiplication `*` and Division `/`
These are evaluated from left to right.
- Addition `+` and Subtraction `-`
These are evaluated next, from left to right.
- Relational & Logical Operators
Comparisons and logical operations are done last.
Example: In the expression `4 + 5 * 2`, the
multiplication `5 * 2` happens first, resulting in `10`. Then the
addition `4 + 10` is performed, giving a final result of `14`.
To change this, you would use parentheses: `(4 + 5) * 2`, which
evaluates to `18`.
For more common math functions, see
Common Math Functions.
Summary: You've learned about the three main
types of operators in Sheerpower: mathematical for calculations,
relational and logical for decision-making, and the all-important
order of operations that ensures your expressions are evaluated
correctly.
(Show/Hide Sheerpower Operators Takeaways)
Sheerpower Operators Takeaways
-
Mathematical operators (+ - * / ^ \ mod) perform numeric
calculations and always return a numeric value.
-
Comparison operators (= <> > < >= <=) return 1 for true and 0
for false, and are used heavily in IF statements and loops.
-
Logical operators (and or not) combine or invert logical
expressions, allowing more complex conditions.
-
Integer division (\) and mod provide exact whole-number results
useful in counters, indexing, and structured data processing.
-
Operators allow programs to evaluate conditions and choose flow,
making them central to decision-making in Sheerpower.
-
Combining mathematical, comparison, and logical operators
enables expressive, compact condition checks.