Skip to content

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
var result int = 5 + 3  // 8

Subtraction (-)

Subtraction
var result int = 10 - 4  // 6

Multiplication (*)

Multiplication
var result int = 6 * 7  // 42

Division (/)

Division
var result int = 15 / 3  // 5

Modulo (%)

Modulo
var result int = 17 % 5  // 2

Advanced Mathematical Operators

Exponentiation (**)

Raises the left operand to the power of the right operand.

Exponentiation
1
2
3
var result int = 2 ** 3   // 8
var result2 int = 5 ** 0  // 1
var result3 int = 3 ** 4  // 81

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
1
2
3
var result int = 17 </ 3   // 5 (regular division would be 5)
var result2 int = 17 </ 4  // 4 (regular division would be 4)
var result3 int = -17 </ 3 // -6 (floors toward negative infinity)

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
var result int = 12 & 10  // 8 (1100 & 1010 = 1000)

Bitwise OR (|)

Performs bitwise OR operation on two integers.

Bitwise OR
var result int = 12 | 10  // 14 (1100 | 1010 = 1110)

Bitwise XOR (^)

Performs bitwise XOR operation on two integers.

Bitwise XOR
var result int = 12 ^ 10  // 6 (1100 ^ 1010 = 0110)

Bitwise NOT (~)

Performs bitwise complement (NOT) operation on an integer.

Bitwise NOT
var result int = ~5  // -6 (inverts all bits)

Left Shift (<<)

Shifts bits to the left by the specified number of positions.

Left Shift
var result int = 5 << 2  // 20 (101 becomes 10100)

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
var result int = 20 >> 2  // 5 (10100 becomes 101)

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
1
2
3
4
5
6
7
8
var x int = 10

x += 5   // x = x + 5  → x = 15
x -= 3   // x = x - 3  → x = 12
x *= 2   // x = x * 2  → x = 24
x /= 4   // x = x / 4  → x = 6
x %= 3   // x = x % 3  → x = 0
x **= 2  // x = x ** 2 → x = 0

Bitwise Compound Assignment

Bitwise Compound Assignment
1
2
3
4
5
6
7
var y int = 15  // 1111 in binary

y &= 7   // y = y & 7   → y = 7  (0111)
y |= 8   // y = y | 8   → y = 15 (1111)
y ^= 3   // y = y ^ 3   → y = 12 (1100)
y <<= 1  // y = y << 1  → y = 24 (11000)
y >>= 2  // y = y >> 2  → y = 6  (110)

Increment and Decrement

Harneet supports both increment/decrement operators and assignment-based forms.

Increment and Decrement
var x int = 5

// Operator forms
var a int = ++x   // pre-increment: x becomes 6, a is 6
var b int = x++   // post-increment: b is 6, x becomes 7
var c int = --x   // pre-decrement: x becomes 6, c is 6
var d int = x--   // post-decrement: d is 6, x becomes 5

// Assignment forms (equivalent, sometimes clearer)
x = x + 1
x += 1
x = x - 1
x -= 1

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 + bigintbigint
  • bigint </ bigintbigint
  • bigint ** bigintbigint (non-negative integer exponents)
  • bigdecimal + bigdecimalbigdecimal
  • bigdecimal </ bigdecimalbigdecimal
  • bigdecimal ** bigdecimalbigdecimal (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
var result int = 10 / 0   // Runtime error: "division by zero"
var result2 int = 10 </ 0 // Runtime error: "division by zero"

Invalid Shift Amounts

Invalid Shift Amounts
var result int = 5 << 64  // Runtime error: "shift amount must be between 0 and 63"
var result2 int = 5 >> -1 // Runtime error: "shift amount must be between 0 and 63"

Type Mismatches

Type Mismatches
var str string = "hello"
var result int = ~str     // Compile error: "bitwise NOT requires integer operand"

Overflow Detection

Overflow Detection
var result int = 2 ** 100  // Runtime error: "integer overflow in exponentiation"

Practical Examples

Bit Manipulation

Bit Manipulation
// Check if number is even
var isEven bool = (number & 1) == 0

// Set bit at position n
var setBit int = value | (1 << n)

// Clear bit at position n
var clearBit int = value & ~(1 << n)

// Toggle bit at position n
var toggleBit int = value ^ (1 << n)

Fast Arithmetic

Fast Arithmetic
1
2
3
4
5
6
7
// Multiply by power of 2
var doubled int = value << 1    // value * 2
var quadrupled int = value << 2 // value * 4

// Divide by power of 2
var halved int = value >> 1     // value / 2
var quartered int = value >> 2  // value / 4

Loop Optimizations

Loop Optimizations
1
2
3
4
5
6
7
8
9
// Efficient counter
for var i = 0; i < 1000; i = i + 1 {
    // Process item i
}

// Countdown loop
for var count = 10; count > 0; count-- {
    // Process countdown
}

Mathematical Calculations

Mathematical Calculations
// Compound interest
var amount int = principal
for var year = 0; year < years; year = year + 1 {
    amount = amount * (100 + rate) // 100
}

// Power calculations
var area int = side ** 2      // Square area
var volume int = side ** 3    // Cube volume
var binary int = 2 ** bits    // Power of 2

Best Practices

  1. Use Parentheses for Clarity

    Use Parentheses for Clarity
    1
    2
    3
    4
    5
    // Clear intent
    var result int = (a + b) * (c - d)
    
    // Relies on precedence (less clear)
    var result int = a + b * c - d
    

  2. Prefer Compound Assignment

    Prefer Compound Assignment
    1
    2
    3
    4
    5
    // Preferred
    counter += increment
    
    // Less efficient and verbose
    counter = counter + increment
    

  3. Choose Appropriate Increment Style

    Choose Increment Style
    1
    2
    3
    4
    5
    // Use pre-increment when you need the new value
    var newValue int = ++counter
    
    // Use post-increment when you need the old value
    var oldValue int = counter = counter + 1
    

  4. Bit Manipulation Safety

    Bit Manipulation Safety
    1
    2
    3
    4
    // Always validate shift amounts
    if shiftAmount >= 0 and shiftAmount < 64 {
        var result int = value << shiftAmount
    }
    

  5. Overflow Awareness

    Overflow Awareness
    1
    2
    3
    4
    // Be careful with large exponentiations
    if base < 100 and exponent < 10 {
        var result int = base ** exponent
    }
    

This comprehensive operator system makes Harneet suitable for systems programming, mathematical computations, and performance-critical applications while maintaining readability and safety.