Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [operators]

Regarding programming languages, operators are constructs which behave generally like functions, but which differ syntactically or semantically from usual functions. From Wikipedia: http://en.wikipedia.org/wiki/Operator_%28programming%29

-3
votes
2answers
130 views

Why was the caret used for exponentiation in BASIC?

As far as I have been able to find, the first language to use ^ for exponentiation was BASIC, in 1964. Earlier languages, such as Fortran, used other symbols such as ** for exponentiation (although in ...
3
votes
1answer
88 views

Should I replace a constant with static methods, if that constant usually 'cooperate' with a specific operator?

For example, to convert between g and kg, I have a constant 1000: public static final float G_TO_KG=1000; . . . this.result = someResult*1000; I found G_TO_KG always bind to operator '*'. So my ...
0
votes
4answers
350 views

Why does C provide both the comma operator and the semicolon to separate statements?

Both the comma operator and the semicolon can be used to separate statements. Let's consider this simple code: #include <stdio.h> void do_something(int*i) {(*i)++;} int main() { int i; ...
56
votes
7answers
8k views

Is there a keyword or operator for “nor”?

Is there an operator equivalent of nor? For example, my favorite color is neither green nor blue. And the code would be equivalent to: // example one if (color!="green" && color!="blue") { ...
61
votes
3answers
8k views

&& and || are not logical but conditional operators?

I am a bit confused by the MSDN C# documentation which states that &,| are logical operators and && and || are conditional operators. I keep calling &&, ||, ! logical operators, ...
2
votes
2answers
195 views

Is implementing operator overloading with variable arity possible?

Implementing operator overloading for e.g. + (plus) isn't terribly difficult if you know it's a binary operator. One can just parse expression + expression But what if the programmer can choose ...
1
vote
0answers
177 views

What programming language first used the 'Safe navigation operator' (?.)?

What programming language first used / invented the 'Safe navigation operator' (?.) ? Unfortunately, the Wikipedia page on this subject does not have information on it and googling yielded no readily ...
1
vote
2answers
160 views

What are the most idiomatic abbreviations for comparison operators [closed]

I am wondering what the most idiomatic text abbreviations for comparison operators are. I need to build a custom enum with them, so I cannot use their symbols. To me personally, these seem idiomatic: ...
0
votes
0answers
103 views

“Canonical” set of operator overloads for a wrapper class

I'm working with a straight-C graphics API (Allegro 5) that defines a union type to hold data generated by various system events, call it "ALLEGRO_EVENT". The C API passes unions of this type to ...
0
votes
4answers
307 views

Is there a way to see how many operations a function, block of code or a statement costs?

Let's say I have a phone that can process 1 million operations per second and a micro controller that can perform 1000. Is there a way to tell how many operations a performed by a function or block ...
3
votes
1answer
1k views

Can I overload operator[][]?

I'm writing a Matrix type, and I would like the following code to define a 2,2 integer matrix, put its first element at 3, and throw an exception upon reaching the third line: Matrix<int> a(2,2)...
1
vote
5answers
905 views

Do increment and decrement operators decrease readability? [closed]

I understand what increment and decrements operators are (++ and --) and the difference between post and pre (i++ vs ++i) but should they be avoided as they do increase the difficulty of reading the ...
4
votes
3answers
141 views

Could one swap the direction of >> and << overloads to define them as class members?

Some operators cannot be overloaded as class members. One such example are the bitwise shift operators used for streams see here for example. The reason for that (as far as I understand it) seems to ...
1
vote
1answer
93 views

Operators precedence

I have a code snippet in Java: int y = ++x * 5 / x-- + --x; So my confusion was since x--(postfix) has higher precedence than ++x(prefix) operator so x-- should be executed first then ++x.But a ...
9
votes
6answers
781 views

In C, is * an operator, or part of a type in a declaration?

In C, * is called the indirection operator or the dereference operator. I understand how it works when it is used in a statement. It makes sense to write *p or * p, considering that it is a unary ...
1
vote
2answers
4k views

IF statement with OR logical operator

Just a basic question on IF statements in programming languages, specifically C++. Consider the following basic code example: int i = 2; if(i == 2 || i == 4) { //do something } Because the ...
0
votes
2answers
274 views

Could Java XYZ implement (limited) Operator Overloading without breaking backwards compatibility?

Recently, I've been learning a bit more of C++ and the dangers and uses of operator overloading, and the readability boost it provides to arithmetic types (like Complex numbers). A while ago, I was ...
1
vote
3answers
399 views

Which programming languages support operators as first class citizens? [closed]

Which programming languages support operators as first class citizens? eg: Return an operator (+, -, =, ==, etc.) from a function, or store within a variable.
33
votes
7answers
2k views

Why do most mainstream languages not support “x < y < z” syntax for 3-way Boolean comparisons?

If I want to compare two numbers (or other well-ordered entities), I would do so with x < y. If I want to compare three of them, the high-school algebra student will suggest trying x < y < z....
47
votes
4answers
9k views

Why do so few languages with a variable-type 'operator' exist?

I mean it in this way: <?php $number1 = 5; // (Type 'Int') $operator1 = +; // (Type non-existent 'Operator') $number2 = 5; // (Type 'Int') $operator2 = *; // (Type non-existent ...
-2
votes
1answer
159 views

What is the difference between these two php operators? [closed]

What is the difference between these 2 PHP operators ? The first one is = and the second one is .=.
64
votes
6answers
9k views

Why is the minus sign, '-', generally not overloaded in the same way as the plus sign?

The plus sign + is used for addition and for string concatenation, but its companion: the minus sign, -, is generally not seen for trimming of strings or some other case other than subtraction. What ...
0
votes
2answers
910 views

Incrementing Strings in PHP (and Perl)

PHP allows you to increment strings. Why? Let's jump ahead a bit. Take the following code: $string = '9ZzZ'; echo ++$string; // 10AaA From a purist point of view this may seem like nonsense, ...
-1
votes
2answers
626 views

How does the post increment operator work in GNU C? [closed]

While the run the below program in Turbo C compiler, I am getting the expected output, however, when I run the same program it using "gcc" compiler in linux, it is giving an unexpected output! int ...
11
votes
1answer
927 views

Why it is not possible to overload compound assignment operator in C#?

The title is be misleading, so please read entire question :-). By "compound assignment operator" I have in mind a construct like this op=, for example +=. Pure assignment operator (=) does not ...
3
votes
1answer
418 views

What is the etymology of the “dot” operator for string concatenation?

Some languages have a . operator for string concatenation. The oldest language I could find that supports it is Perl. Was Perl the first to use it? Why was it chosen?
87
votes
7answers
11k views

Short circuit evaluation, is it bad practice?

Something that I've known for a while but never considered is that in most languages it is possible to give priority to operators in an if statement based on their order. I often use this as a way to ...
13
votes
2answers
7k views

When should I use C#'s implicit type conversion operator?

In C#, we can overload the implicit conversion operator like this (example from MSDN): struct Digit { /* ... */ public static implicit operator byte(Digit d) // implicit digit to byte ...
1
vote
1answer
658 views

What is the “->” symbol called?

I have seen the -> operator/symbol in Java 8 predicates recently and wondered what its name is. I know that it is used in lambda expressions, but I know that the symbol for lambda is λ, so that's ...
3
votes
1answer
599 views

Well defined mathematical operations for bearing (angle) class

I have written a class to represent bearings (angles with a nautical theme, and a specific normalisation range). In the program, it is necessary to perform some mathematical operations on them, so I'...
0
votes
2answers
236 views

Missing `$` like operators in Haskell

Below I've produced what I believe to be a summary of the standard $ like operators for various classes in Haskell. There's some gaps however. Following the applicative pattern, you would think those ...
4
votes
2answers
522 views

Implicit Conversion Operators for Value Types in C#

I’ve come across this blog article: Implicit Conversion Operators are Bad. The article discourages the use of implicit conversion with reference types. The article describes problems caused by ...
3
votes
1answer
770 views

How do you set and check a single bit in a programming language that does not support bitwise operations?

I'm using a programming language (more a scripting language) that does not support any bitwise operators like AND, OR, XOR, NOT (and shift as well). Common arithmetic and logical operations like + - *...
0
votes
1answer
171 views

Is it correct to call the assignment symbol an “operator” when it is actually a statement?

In some languages (C++, Java, Ruby, etc.) an assignment returns a value and can be used in an expression: x = (y = z); // ok, 'x' gets the value of 'z' In other languages (Ada, VHDL), an ...
3
votes
4answers
974 views

Lexing: One token per operator, or one universal operator token?

When lexing, what would be the best way to tokenize operators? Would one just create a BinaryOperator token, or a separate token for every single binary operator? Examples: PlusOperator, MinusOperator,...
8
votes
3answers
13k views

Speeds of << >> multiplication and division

You can use << to multiply and >> to divide numbers in python when I time them I find using the binary shift way of doing it is 10x faster than dividing or multiplying the regular way. ...
29
votes
3answers
5k views

Why does the boolean type in C++ support ++ but not --?

Why does the operator -- not exist for bool whereas it does for operator ++? I tried in C++, and I do not know if my question apply to another language. I will be glad to know also. I know, I can ...
26
votes
9answers
9k views

What is the benefit of having the assignment operator return a value?

I'm developing a language which I intend to replace both Javascript and PHP. (I can't see any problem with this. It's not like either of these languages have a large install base.) One of the things ...
0
votes
5answers
583 views

Why is it bad to use redundancy with logical operators? [duplicate]

I'm moving over to work on a library that a fellow developer has been writing. It's full of == true and == false, which I find crazy frustrating to read. I've tried asking him to quit doing it, but ...
3
votes
3answers
1k views

Specifics of Switch and If statements

Why do we need the switch statement if there is the if statement? Why can't we use several ifs like if(a==1) do this1; if(a==2) do this2; ... instead of switch (a) { case(1): {do ...
5
votes
2answers
759 views

Why isn't exponentiation hardware-implemented?

Why is there no exponentiation operation in hardware, even though many languages have builtin operators for it? Is it because even hardware implementations would need to use the same algorithm as ...
5
votes
2answers
905 views

Languages supporting unicode logic operators

Are there any programming languages that support the use of unicode logic operators? For example, many programming languages use "!=" as the "does not equal" operator, but in mathematics the symbol ...
1
vote
2answers
726 views

Should ** bind more tightly than !, ~?

Designing a programming language, I'm including the ** exponentiation operator. In Fortran and Python, the two languages I know of which have this operator, it binds more tightly than unary minus, ...
12
votes
4answers
10k views

Instantiating Null Objects with Null-Coalescing Operator

Consider the following typical scenario: if(myObject == null) { myObject = new myClass(); } I'm wondering what is thought of the following replacement using the null-coalescing operator: ...
0
votes
3answers
669 views

Any languages use the '=>' and '=<' operators? [closed]

I'm just curious if there are any programming languages that use the '=>' and '=<' operators for something. I wonder why these are not commonly used when their variant '<=' and '>=' are so ...
6
votes
2answers
4k views

What do you call parentheses operators?

In most programming languages documentation where they talk about operators +-*/=<> they also include ()[] as operators. There are unary operators, multiplicative operators, additive operators, ...
1
vote
1answer
103 views

Operator distributivity in expressions - syntactic sugar

When writing boolean expressions out, you often have to use something of the type: A == any of B1, ..., Bn C < all of D1, ..., Dm But in code you have to use expanded forms: A == B1 or ... or A =...
0
votes
3answers
710 views

Using Power of 2 numbers to represent types

Let's say that we have some values, represented by power of 2: TYPE_1 = 1 TYPE_2 = 2 TYPE_3 = 4 TYPE_4 = 8 ... I need to store some of these types in one value. Example: To represent TYPE_1 with ...
1
vote
1answer
180 views

How does the ? make a quantifier lazy in regex [closed]

I've been looking into regex lately and figured that the ? operator makes the *,+, or ? lazy. My question is how does it do that? Is it that *? for example is a special operator, or does the ? have ...
59
votes
2answers
5k views

Why do bitwise operators have lower priority than comparisons?

Could someone explain the rationale, why in a bunch of most popular languages (see note below) comparison operators (==, !=, <, >, <=, >=) have higher priority than bitwise operators (&, |, ^...