Skip to main content

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

Filter by
Sorted by
Tagged with
0 votes
1 answer
582 views

In C/C++ and Java, why does dividing an integer by an integer return an integer, while in other languages, it returns a decimal value? [closed]

I have found another similar question, but the answer there talks mainly about C/C++, and does not mention about the division operator. So, in C/C++ and Java, dividing an integer by an integer returns ...
58 votes
8 answers
19k 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") { ...
9 votes
2 answers
2k views

Which are the fundamental stack manipulation operations?

I'm creating a stack oriented virtual machine, and so I started learning Forth for a general understanding about how it would work. Then I shortlisted the essential stack manipulation operations I ...
9 votes
4 answers
16k views

Do any languages use =/= for the inequality operator?

Wikipedia says: Not equal The symbol used to denote inequation — when items are not equal — is a slashed equals sign "≠" (Unicode 2260). Most programming languages, limiting themselves to ...
1 vote
3 answers
415 views

What is the problem with a range operator?

Virtually all (imperative) languages offer the operators < <= >= > for lower, lower or equal, greater or equal and greater. But why no ternary range operator like (10 < x < 100) to ...
2 votes
3 answers
436 views

C# Duplicated usage of an if/else condition and a ternary operator. A good practice?

I had a debate with a work mate regarding the following code as to which one of it would be the better practice: My code (in pseudo): var A = <precondition either TRUE or FALSE>; var B; if(A) { ...
49 votes
9 answers
19k views

Why didn't == operator string value comparison make it to Java?

Every competent Java programmer knows that you need to use String.equals() to compare a string, rather than == because == checks for reference equality. When I'm dealing with strings, most of the ...
13 votes
1 answer
1k 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 ...
8 votes
4 answers
29k views

Ternary operator (condition ? foo : bar) and the XOR (^) operator

I have read in a recent code review that both ternary operator (condition ? foo : bar) and the XOR operator ^ are rarely used in Java. Is it true? If yes, is this because they are less readable? or ...
0 votes
1 answer
257 views

Excessively verbose and cryptic comparisons in Java

I don't know if this is the right place to ask more of a "philosophical" question. The more I code in Java, the more I have to bear with Comparable<T>. And the more I bear with this ...
98 votes
16 answers
33k views

Why aren't user-defined operators more common? [closed]

One feature I miss in from functional languages is the idea that operators are just functions, so adding a custom operator is often as simple as adding a function. Many procedural languages allow ...
6 votes
3 answers
8k 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, ...
43 votes
10 answers
9k 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....
0 votes
4 answers
260 views

Language design : use equals symbol = both for affectation and comparison, like in MySQL

I'm currently designing a database query language and I came to wonder what should be the best syntax for the comparison operator. Most modern languages use ==, but amongst the database languages ...
18 votes
3 answers
2k views

Should wrappers compare as equal using the == operator when they wrap the same object?

I'm writing a wrapper for XML elements that allows a developer to easily parse attributes from the XML. The wrapper has no state other than the object being wrapped. I am considering the following ...
27 votes
9 answers
14k 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 ...
3 votes
1 answer
202 views

Should equality operators be approximate when defining numeric types that rely on floating-point numbers?

For example, say you have a class Point which has floating point components. It's tempting to overload the equality operator so you can do something like a = Point(1.1, 2.2) b = Point(3.3, 6.6) b /= ...
62 votes
3 answers
11k views

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

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

Why don't languages use the words "and" and "or" instead of "&&" and "||"?

When I was a beginner it took a while to learn the language syntax and the idea that languages couldn't improve after they were invented. But now we're seeing new language features added every year ...
2 votes
1 answer
257 views

Good way to do 3D vector math in language without operator overloading

I would like to make a simple web application (a static website where all computation happens on the client) that generates a mesh and displays it. I have a working prototype in Unity and now I'm ...
1 vote
2 answers
1k views

Solving issues in using post and pre increment operators as part of expressions

I recently had a discussion with a friend about code maintainability with regards to modifying an iterator inside of the body of a loop (C# syntax): List<int> test = new List<int>(); for (...
0 votes
2 answers
261 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 ...
0 votes
2 answers
879 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 ...
93 votes
7 answers
17k 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 ...
3 votes
1 answer
110 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 ...
2 votes
4 answers
4k 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; ...
7 votes
4 answers
13k views

Can I change operator precedence and associativity in C++?

As the title says, I find it useful to be able to overload operators. Is it possible to also change the way the operators are parsed by specifying the precedence and associativity of overridden ...
31 votes
3 answers
8k 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 ...
2 votes
2 answers
518 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 ...
2 votes
0 answers
410 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
2 answers
2k 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: ...
73 votes
2 answers
8k 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 (&, |, ^...
60 votes
3 answers
103k views

What is the name of ** in python?

When programming Python I sometimes do a ** to make a conversion. I understand what it does but what data structures am I manipulating? A dict and what is the other? An array? Is there a name for the *...
0 votes
4 answers
2k 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
1 answer
6k 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)...
3 votes
5 answers
2k 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
3 answers
188 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 ...
14 votes
3 answers
42k 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. ...
0 votes
5 answers
1k 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 ...
9 votes
6 answers
2k 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 ...
2 votes
2 answers
31k 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
2 answers
455 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
3 answers
870 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.
44 votes
4 answers
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 ...
-1 votes
1 answer
166 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 .=.
9 votes
2 answers
1k 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 ...
62 votes
6 answers
13k 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 ...
1 vote
2 answers
2k 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, ...
89 votes
25 answers
48k views

Ternary operator considered harmful? [closed]

For example, would you prefer this one-liner int median(int a, int b, int c) { return (a<b) ? (b<c) ? b : (a<c) ? c : a : (a<c) ? a : (b<c) ? c : b; } or an if/else solution ...
-1 votes
2 answers
1k 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 ...