Questions tagged [binary-operators]

Use this tag for questions that have to do with operators that are identified as binary operators, i.e. operators that work with the two operands.

A binary operator is an operator that operates on two operands and manipulates them to return a result. Operators are represented by special characters or by keywords and provide an easy way to compare numerical values or character strings.

Binary operators are presented in the form:

Operand0 Operator Operand1

Source

204 questions
1539
votes
10 answers

What are bitwise shift (bit-shift) operators and how do they work?

I've been attempting to learn C in my spare time, and other languages (C#, Java, etc.) have the same concept (and often the same operators)... At a core level, what does bit-shifting (<<, >>, >>>) do, what problems can it help solve, and what…
150
votes
1 answer

Why does the C# compiler translate this != comparison as if it were a > comparison?

I have by pure chance discovered that the C# compiler turns this method: static bool IsNotNull(object obj) { return obj != null; } …into this CIL: .method private hidebysig static bool IsNotNull(object obj) cil managed { ldarg.0 // obj …
stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
19
votes
2 answers

Playing with references

I can see why $a = new ArrayObject(); $a['ID'] = 42; $b = &$a['ID']; $c = $a; $c['ID'] = 37; echo $a['ID']."\n"; echo $b."\n"; echo $c['ID']."\n"; outputs 37, 42, 37 while $a = new ArrayObject(); $a['ID'] = 42; $b = &$a['ID']; $c = $a; $b =…
Mmmh mmh
  • 5,334
  • 3
  • 21
  • 29
16
votes
6 answers

Java 8 - store lambdas in List

I wonder if it's possible to store lambdas in some container, for ex. ArrayList or HashMap. I want to change that code: public enum OPCODE implements BinaryOperator { MOV((x, y) -> y), INC((x, y) -> ++x), DEC((x, y) -> --x), …
Jump3r
  • 1,028
  • 13
  • 29
13
votes
2 answers

Understanding an overloaded operator[] example

I am confused with a question that I saw in a c++ test. The code is here: #include using namespace std; class Int { public: int v; Int(int a) { v = a; } Int &operator[](int x) { v+=x; return *this; …
BugShotGG
  • 5,008
  • 8
  • 47
  • 63
10
votes
4 answers

Why does this bitwise shift-right appear not to work?

Could someone explain to me why the mask is not shifted to the right at all? You can use anything in place of that 1 and the result will be the same. unsigned mask = ~0 >> 1; printf("%u\n", mask);
Ree
  • 6,061
  • 11
  • 48
  • 50
10
votes
5 answers

c++ multiple enums in one function argument using bitwise or "|"

I recently came across some functions where you can pass multiple enums like this: myFunction(One | Two); Since I think this is a really elegant way I tried to implement something like that myself: void myFunction(int _a){ switch(_a){ …
moka
  • 365
  • 1
  • 5
  • 10
8
votes
2 answers

Replacing switch by BinaryOperator

I'm trying to replace the common switch for arithmetical operations by BinaryOperator functional interface. The base method is: private static int computeOne(int res, String operand, String operation) { int number =…
8
votes
3 answers

Is my understanding of monoid valid?

So, I'm learning Haskell at the moment, and I would like to confirm or debunk my understanding of monoid. What I figured out from reading CIS194 course is that monoid is basically "API" for defining custom binary operation on custom set. Than I…
Reygoch
  • 1,204
  • 1
  • 11
  • 24
8
votes
3 answers

No binary operators for structured arrays in Numpy?

Okay, so after going through the tutorials on numpy's structured arrays I am able to create some simple examples: from numpy import array, ones names=['scalar', '1d-array', '2d-array'] formats=['float64', '(3,)float64', '(2,2)float64'] my_dtype =…
user2789194
  • 135
  • 4
7
votes
5 answers

More efficient algorithm to find OR of two sets

Given a matrix of n rows and m columns of 1's and 0's, it is required to find out the number of pairs of rows that can be selected so that their OR is 11111....m times. Example: 1 0 1 0 1 0 1 0 0 1 1 1 1 1 0 Answer: 2 ---> OR of row number…
yobro97
  • 1,125
  • 8
  • 23
7
votes
1 answer

Unary And Binary Minus in Parse Tree

I am creating a parse tree that will contain expressions similar to 3 - 4 * 8 or 8 * -5 or -(10 * 1) I need a way to distinguish between the unary and binary minus. The way my grammar is going now the binary minus is reached first, but I am…
6
votes
1 answer

In Rust, is "as" an operator?

The Rust Reference presently says the following about the as operator: 7.2.12.5 Type cast expressions A type cast expression is denoted with the binary operator as. Executing an as expression casts the value on the left-hand side to the type on the…
Thanatos
  • 42,585
  • 14
  • 91
  • 146
6
votes
2 answers

C++ - How to use a vector of reference_wrapper

I'm trying to refactor part of a pathfinding algorithm I had that used pointers to not use pointers. Unfortunately I'm not that knowledgable about references. I get the error: Invalid operands to binary expression…
Evan Ward
  • 1,371
  • 2
  • 11
  • 23
6
votes
2 answers

What is the Java 8 reduce BinaryOperator used for?

I am currently reading the O'Reilly Java 8 Lambdas, it is a really good book. I came across with a example like this. I have a private final…
chiperortiz
  • 4,751
  • 9
  • 45
  • 79
1
2 3
13 14