Skip to main content

Questions tagged [operators]

For questions about operators in programming languages. Operators are built-in tokens that perform specific operations on their operands distinct from functions.

Filter by
Sorted by
Tagged with
4 votes
2 answers
319 views

Can two infix operators have the same priority, but different associativity? If so, how would it be implemented in the parser?

I am interested, is it possible that, in some programming language, two infix operators have the same priority, but different associativity? If so, how is that implemented in the parser?
FlatAssembler's user avatar
11 votes
4 answers
5k views

How do you correctly compile the chained comparison operators like ones that exist in Python (`a < b < c`), if `b` might have side-effects?

The most obvious way of supporting chained comparisons operators, such as a < b < c, is to rewrite it as ...
FlatAssembler's user avatar
2 votes
1 answer
158 views

How is the VHDL operator `<=`, which can be both right-associative (signal assignment) and left-associative (less-than-or-equal), parsed?

One of the most confusing things to me about VHDL is that the <= operator can mean both "less than or equal to" and it can be a signal assignment ...
FlatAssembler's user avatar
10 votes
3 answers
415 views

What should be the precedence of the bitwise operators relative to each others?

In C, the relative precedence of bitwise operators is as follows, from high to low precedence: ...
WhiteMist's user avatar
  • 281
8 votes
6 answers
4k views

Why do relational comparison operators never short-circuit?

I just thought about the possibility for the less-than and-greater than operators to short-circuit. That is, they can skip evaluating their second operand if the value of the second operand logically ...
CPlus's user avatar
  • 8,261
45 votes
6 answers
13k views

Why do programming languages use the asterisk * for multiplication?

Having had very little math(s) education I'm trying to bring myself up to speed for university, which currently involves teaching myself the times tables, where X ...
Hashim Aziz's user avatar
3 votes
4 answers
280 views

Common postfix representations of pointer/reference operations

In some C-based languages (including C/C++ themselves and Rust), taking pointers/references and dereferencing is done with prefix operators & and ...
abel1502's user avatar
  • 2,497
12 votes
5 answers
910 views

Prior art on pipelines of function calls

Say I have a loop that looks like this: for i in range(1, 10) { print(i) } Now I want to take the same sequence in reverse, and filter out even numbers. I might ...
Rob N's user avatar
  • 1,083
19 votes
3 answers
4k views

Why does MATLAB have left division/solve?

In MATLAB (and Octave), the \ (or mldivide) and \. operators are provided with the exact ...
Adám's user avatar
  • 3,138
17 votes
9 answers
5k views

Why add an increment/decrement operator when compound assignments exist?

Many languages have compound assignment statements, for example +=. However, for the common task of adding or subtracting 1 from a variable some languages (mostly ...
Seggan's user avatar
  • 2,673
9 votes
4 answers
496 views

What are the advantages of keyword based syntax versus punctuation based syntax?

Some languages such as Lua and Assembly Basic use keywords for syntax. This includes spelling out words such as do and end in ...
CPlus's user avatar
  • 8,261
2 votes
3 answers
232 views

Pros and cons of generalized `nil`-coalescing operators

Many languages extend their "indexing" operator . to a nil-coalescing variant ?. to deal with indexing chains such as <...
Luatic's user avatar
  • 330
6 votes
6 answers
363 views

Ways to have operators for both normal and floor division

I want to have an operator for true division (like / in python) giving a float as the result and floor division (like // in ...
MarcellPerger's user avatar
9 votes
6 answers
3k views

What syntax could be used to implement both an exponentiation operator and XOR?

C does not have an exponentiation operator, so that freestanding implementations that lack the math libraries could lack exponentiation as a whole, and still be permissible by the C standard. As a ...
CPlus's user avatar
  • 8,261
19 votes
7 answers
2k views

What are the downsides of supporting chained comparison operators?

Some languages, such as Python, support chaining comparison operators, such that x < y < z is equivalent to ...
Rydwolf Programs's user avatar
4 votes
9 answers
1k views

What are pros/cons of ternary conditional operators?

There are some languages that support ternary operator. There are Python, C, JavaScript, PHP. In languages like C and JavaScript, the syntax is like this: ...
чистов_n's user avatar
3 votes
3 answers
108 views

Pre-increment and post-increment and augmented assignment but no post-augmented assignment?

In C-style languages we have: int x = 10; x++; // x is now 11 but the expression returns 10 ...
CPlus's user avatar
  • 8,261
11 votes
2 answers
481 views

What are some caveats to be careful about when implementing the `?:` (ternary conditional) operator?

Thus far, I see two things that can go wrong when implementing the ?: (ternary conditional) operator: PHP famously incorrectly implemented the ...
FlatAssembler's user avatar
6 votes
2 answers
409 views

If size could be determined at compile time then why could size not be determined at preprocess time?

In C the sizeof() operator cannot be resolved in preprocessor conditions. If other operators such as + or ...
CPlus's user avatar
  • 8,261
8 votes
4 answers
403 views

What are the potential problems with assignment operators returning the new-value operand and how can they be avoided or mitigated?

Inline assignment is when the assignment operator can return the value of the new-value (right-hand) operand. For example: ...
CPlus's user avatar
  • 8,261
8 votes
3 answers
360 views

Unambiguous syntax for a ternary comparison operator

How could an operator to test if a number is within a lower and upper bound? SQL seems to have this as in 10 <= x <= 20. However, if this were to be ...
CPlus's user avatar
  • 8,261
14 votes
7 answers
677 views

Ternary operators other than ?: [closed]

In many programming languages, there is a ternary operator such as X ? Y : Z or Y if X else Z. This can be used as an ...
FireTheLost's user avatar
  • 1,603
12 votes
6 answers
424 views

What are the disadvantages of this far-fetched idea: All integers can be treated as boolean arrays?

I do not know of any languages that support this idea but I just thought that since numbers are just bits, why should we not be able to access those bits directly? In most languages such as C or C++, ...
CPlus's user avatar
  • 8,261
9 votes
1 answer
189 views

How to distinguish custom operators from each other?

How might one go about building a parser that can distinguish custom operators in prefix, infix or postfix positions. For example: ...
kaiserthe13th's user avatar
5 votes
2 answers
549 views

What are the pros and cons of each type of operator notation (prefix/postfix/infix)?

In Prefix notation, the operator comes before the operands (e.g., +a b). In Postfix notation, the operator comes after the operands (e.g., ...
The Thonnu's user avatar
  • 1,508
2 votes
3 answers
764 views

Why is the Elvis operator not more common?

From Wikipedia: In certain computer programming languages, the Elvis operator, often written ?:, is a binary operator that returns its first operand if that ...
justANewbie's user avatar
13 votes
5 answers
721 views

Distinguishing modulo (Euclidean division) from remainder

The % operator in most programming language provide a remainder from integer division. When both operands are positive all works as expected. But when the number is ...
CPlus's user avatar
  • 8,261
7 votes
5 answers
467 views

Why are expressions in languages so prone to integer overflow?

I've always wondered. Why do major languages let their addition and multiplication operators be so prone to integer overflow? My programming language would have separate operators to handle overflow. ...
Dannyu NDos's user avatar
11 votes
6 answers
686 views

How can ambiguity with unary and binary operators be resolved?

Most languages use +/- for addition and subtraction, in addition to using +/...
Rydwolf Programs's user avatar
7 votes
6 answers
522 views

Advantages/disavantages of including the comma operator?

In many C style languages the , binary operator takes 2 expressions and simply returns the second one: ...
mousetail's user avatar
  • 8,229
17 votes
15 answers
1k views

What are the syntax options for implementing a ternary "if" operator?

The ternary "if" operator is a syntactic construct for returning one of two values based upon a boolean condition. In most C-family languages its syntax resembles ...
Ginger's user avatar
  • 2,619
14 votes
11 answers
934 views

What alternatives are there for C++ operator overloading syntax?

I noticed C++ has operator overloading syntax with just the operator names: T& operator +(T& lhs, T& rhs) { return /* ... */; } However, there are ...
CPlus's user avatar
  • 8,261