Advanced Arithmetic Operators in Harneet
This document provides a comprehensive reference for all arithmetic operators in the Harneet programming language, including the newly added advanced operators.
Operator Precedence Table
The following table shows all operators in order of precedence (highest to lowest):
| Precedence | Operators | Description | Associativity |
|---|---|---|---|
| 15 | [] | Array/map indexing | Left-to-right |
| 14 | () . | Function calls, field access | Left-to-right |
| 13 | ++ -- | Postfix increment/decrement | Left-to-right |
| 12 | ++ -- ~ - not | Prefix increment/decrement, bitwise NOT, unary minus, logical NOT | Right-to-left |
| 11 | ** | Exponentiation | Right-to-left |
| 10 | * / % </ | Multiplication, division, modulo, floor division | Left-to-right |
| 9 | + - | Addition, subtraction | Left-to-right |
| 8 | << >> | Bit shifts | Left-to-right |
| 7 | < <= > >= | Comparison | Left-to-right |
| 6 | == != | Equality | Left-to-right |
| 5 | & | Bitwise AND | Left-to-right |
| 4 | ^ | Bitwise XOR | Left-to-right |
| 3 | \| | Bitwise OR | Left-to-right |
| 2 | and | Logical AND | Left-to-right |
| 1 | or | Logical OR | Left-to-right |
Basic Arithmetic Operators
Addition (+)
| Addition | |
|---|---|
Subtraction (-)
| Subtraction | |
|---|---|
Multiplication (*)
| Multiplication | |
|---|---|
Division (/)
| Division | |
|---|---|
Modulo (%)
| Modulo | |
|---|---|
Advanced Mathematical Operators
Exponentiation (**)
Raises the left operand to the power of the right operand.
| Exponentiation | |
|---|---|
Important Notes: - Right-associative: 2 ** 3 ** 2 equals 2 ** (3 ** 2) = 512 - Negative exponents not supported for integer bases - Overflow detection for large results
Floor Division (</)
Performs division and rounds down to the nearest integer.
| Floor Division | |
|---|---|
Difference from Regular Division: - Regular division (/) truncates toward zero - Floor division (</) always rounds toward negative infinity
Bitwise Operators
Bitwise AND (&)
Performs bitwise AND operation on two integers.
| Bitwise AND | |
|---|---|
Bitwise OR (|)
Performs bitwise OR operation on two integers.
| Bitwise OR | |
|---|---|
Bitwise XOR (^)
Performs bitwise XOR operation on two integers.
| Bitwise XOR | |
|---|---|
Bitwise NOT (~)
Performs bitwise complement (NOT) operation on an integer.
| Bitwise NOT | |
|---|---|
Left Shift (<<)
Shifts bits to the left by the specified number of positions.
| Left Shift | |
|---|---|
Validation: - Shift amount must be between 0 and 63 - Equivalent to multiplying by 2^n
Right Shift (>>)
Shifts bits to the right by the specified number of positions.
| Right Shift | |
|---|---|
Validation: - Shift amount must be between 0 and 63 - Equivalent to dividing by 2^n (for positive numbers)
Compound Assignment Operators
Compound assignment operators perform an operation and assign the result back to the variable.
Arithmetic Compound Assignment
| Arithmetic Compound Assignment | |
|---|---|
Bitwise Compound Assignment
| Bitwise Compound Assignment | |
|---|---|
Increment and Decrement
Harneet supports both increment/decrement operators and assignment-based forms.
Type Compatibility
Integer Operations
All bitwise operators, shifts, and increment/decrement work only with integer types: - int, int8, int16, int32, int64 - uint, uint8, uint16, uint32, uint64, uintptr
bigint and bigdecimal participate in arithmetic (+, -, *, /, %, </, **) and comparisons, but only in same-type operations:
bigint + bigint→bigintbigint </ bigint→bigintbigint ** bigint→bigint(non-negative integer exponents)bigdecimal + bigdecimal→bigdecimalbigdecimal </ bigdecimal→bigdecimalbigdecimal ** bigdecimal→bigdecimal(non-negative integer exponents; exponent must be an integer value)
There is no implicit mixing between bigint/bigdecimal and fixed-size integers or floats. Use the bigint(...) or bigdecimal(...) constructors, or the cast module, to convert explicitly when needed.
Mixed Type Operations (fixed-size)
- Integer + Float → Float result
- Exponentiation with float operands → Float result
- Floor division always returns integer result
Error Handling
Division by Zero
| Division by Zero | |
|---|---|
Invalid Shift Amounts
| Invalid Shift Amounts | |
|---|---|
Type Mismatches
| Type Mismatches | |
|---|---|
Overflow Detection
| Overflow Detection | |
|---|---|
Practical Examples
Bit Manipulation
| Bit Manipulation | |
|---|---|
Fast Arithmetic
| Fast Arithmetic | |
|---|---|
Loop Optimizations
| Loop Optimizations | |
|---|---|
Mathematical Calculations
| Mathematical Calculations | |
|---|---|
Best Practices
-
Use Parentheses for Clarity
-
Prefer Compound Assignment
-
Choose Appropriate Increment Style
-
Bit Manipulation Safety
-
Overflow Awareness
This comprehensive operator system makes Harneet suitable for systems programming, mathematical computations, and performance-critical applications while maintaining readability and safety.