# Questions tagged [operators]

5570 questions

1

votes

1

answer

71

Views

### What's a fast way to space-out bits within a word?

I have a 32-bit value in the lower part of a 64-bit register; the top part is 0 (X denotes a bit with information, bits listed from LSB to MSB):
X X X ... X 0 0 0 0 ... 0
Now, I want to 'space out' the bits with information, so that I have
X 0 X 0 X 0 ... X 0
(or if you'd rather put the 0's first,...

0

votes

2

answer

21

Views

### fastest way to minimize n as per given condition in python

how can i make the following code faster? the input is a binary string and i am converting it to a number. possibly i need to use the binary string instead of the number? the algorithm may only divide by 2 or subtract 1 from the input. I tried the code below and it is not fast enough.
I tried the b...

1

votes

1

answer

24

Views

### How to query an array for different combinations of ANDs and ORs in MongoDB Aggregation?

I am trying to search for list of students who have completed courses using different combinations of Logical ANDs and ORs.
For example I want to get student who completed ('Course 1' and 'Course 2') or 'Course 3'
Here is my data structure for each student :
{
'_id' : ObjectId('5c68841cb6b18f31975c...

1

votes

1

answer

77

Views

### How to check if a subset is included into a set? [duplicate]

This question already has an answer here:
How to compare sets of enumerated types
2 answers
I have two set type variables and I need to check if the first is a subset of the second one.
type
TMyValue = (mvOne, mvTwo, mvThree);
TMyValues = set of TMyValue;
...
var
V1 : TMyValues;
V2 : TMyValues;
beg...

1

votes

3

answer

43

Views

### How to map multiple properties to arrays in Angular 6?

I have an array of objects like:
const data: any[] = [
{ x: 1, y: 1 },
{ x: 2, y: 2 },
{ x: 3, y: 4 },
{ x: 4, y: 6 }
];
// get x as array
from(d).pipe(map(m => m.x), toArray()).subscribe(x => ...);
And would like to map it to something like below to use it in Plotly
{
x: [1,2,3,4],
y: [1,2,4,6]
}...

0

votes

1

answer

28

Views

### What is logical and of two Integers? [duplicate]

This question already has an answer here:
Python's Logical Operator AND
6 answers
x = 100
y = 200
z = x and y
print (z)
I want to ask what type of and this is?
It is not bitwise and. Python gives me 200. How?

0

votes

1

answer

21

Views

### Observable best practice

In my current project I have an observable returning values and I have to update my local variable according to the value returned by the observable.
I see two ways of doing this, either by directly subscribing to my observable, or by using pipable operator.
According to your experience/knowledge, w...

1

votes

1

answer

31

Views

### How is == method in scala is different from == operator in java? [duplicate]

This question already has an answer here:
What's the difference between == and .equals in Scala?
5 answers
What is the difference between == and equals() in Java?
22 answers
== operator in Java compares two object references and whether or not they are are pointing to same object references,it res...

1

votes

2

answer

28

Views

### Replace(), replacing a value not specified

I've been learning Javascript via FCC for 6 weeks now, and decided to spend a week learning, and playing around with methods to get really close and comfortable with using them in loops, and with statements. Just playing around with strings, I was trying to replace the string 'I like milk', with 'I...

1

votes

4

answer

87

Views

### Python: Why do my 'if' statements' evaluate numbers incorrectly? [closed]

The code is as follows:
from random import *
numStars = int(input('Number of stars: ').strip())
planetTypeDict = {'O':0, 'B':0, 'A':0, 'F':0, 'G':0, 'K':0, 'M':0}
for star in range (numStars):
planetTypeChanceNum = uniform (0, 100)
if planetTypeChanceNum < 76.45:
planetType = 'M'
planetTypeDict['M'...

1

votes

1

answer

96

Views

### What are the operators ==> and =?>?

I was reading F# code when I found two operators I was not aware of.
'Clean'
==> 'ResolveDependencies'
==> 'SetBuildNumber'
=?> ('PatchAssemblyInfo', buildServer = TeamCity)
I looked into the F# documentation page for the operators without success. I did not find anything about them. What are they?

1

votes

4

answer

82

Views

### How does bitwise AND OR and XOR works on -negative signed integers?

I was just solving random problems on bitwise operators and trying various other combination for making personal notes. And somehow I just cannot figure out the solution.
Say I wanted to check bitwise AND between two integers or on a ~number and -negative number(~num1 & -num2) and various other comb...

1

votes

4

answer

87

Views

### Nullable bool logical “AND” operator behaves differently with true and false [duplicate]

This question already has an answer here:
Nullable: and overloaded operators, bool? & bool
4 answers
Using bool? variables, can somebody explain why true & null results in null and false & null results in false?
void Main()
{
bool? a = null;
bool? b = true;
bool? c = a & b;
Console.WriteLine($'...

1

votes

2

answer

45

Views

### Create a literal operator function that takes 2 input parameters

I have a defined type Coordinates like so:
#include
using Coordinates = std::array;
For which I call the following operator overload functions:
Coordinates operator+(const Coordinates& lhs, const Coordinates& rhs);
Coordinates operator*(const Coordinates& lhs, const Coordinates& rhs);
both overload...

1

votes

1

answer

54

Views

### Is there a way to make functions take in an operator as an argument in c++?

Let's suppose I have a function as follows:
#include
int func(int a, int b)
{
return a+b;
}
Without using an if else construct, suppose I want to generalize this function to take in an operator '-' that allows me to return a-b , is there a way for me to do it?
I came across the following link with...

1

votes

2

answer

69

Views

### Performance of array lookup vs bitshift

For an implementation of Othello (Reversi), I precomputed powers of two to be looked up in an array. Today, while trying to optimize for speed, this struck me as a premature optimization since I previously assumed lookup to be faster and never actually made a benchmark.
Here's how i precalculate the...

0

votes

2

answer

24

Views

### bash IF condition with OR logic syntax

i am confused..
i just make if file does not exist OR md5 sum is wrong
something like this
md5=20ffa23fbb589484d03d79608fe8d2ad
if ! -f /tmp/1.txt || echo -n ${md5} /tmp/1.txt | md5sum --status --check -
then
....
fi
how write valid syntax?

1

votes

0

answer

249

Views

### Elementwise bitwise-XOR numpy matrix with numpy vector

Suppose that we are given a two dimensional matrix A of dtype=uint8 with N rows and M columns and a uint8 vector of size N called x. We need to bit-wise XOR each row of A, e.g. A[i], with the corresponding element in x, i.e. x[i].
Currently, I am doing this as follows, but think that there are more...

1

votes

0

answer

25

Views

### Any way to highlight operators comparison for Objects in iOS Objective-C project

Recently in a iOS project I have refactored money data type from double to NSDecimalNumber to get a rid of discrepancies caused by handling money with doubles.
The refactoring is complete but my concerns are for all those comparisons made through standard operators that are not raised as errors by t...

1

votes

1

answer

166

Views

### How to replicate PEXT (parallel bit extract) with basic bitwise ops?

I'm porting some code from Rust to Swift and I noticed they use pext method to do some bit manipulation. Since it's an Intel instruction I can't get that into my Swift code so I was wondering what set of bitwise instructions (that I can use in Swift) would be able to get the same result as a PEXT op...

1

votes

1

answer

70

Views

### Operator overloading, definition/header for all operators

How best to split the following iterator code into a header and definition.
// std::iterator example
#include // std::cout
#include // std::iterator, std::input_iterator_tag
class MyIterator : public std::iterator
{
int* p;
public:
MyIterator(int* x) :p(x) {}
MyIterator(const MyIterator& m...

1

votes

0

answer

40

Views

### Something doesn't work. What did I do wrong, if anything? How can I solve my issue?

Last weekend I created on my home computer with Access a database containing only one table, 'People', which contained the following field: 'First name', 'Last name', 'Gender', 'Date of birth', 'Race', 'Civil status', 'Blood type' (I generated the records through fakepersongenerator).
Now, I wanted...

1

votes

1

answer

56

Views

### Why does this and/or scenario evaluate to false?

Why does this return false?
var flavor = 'chocolate';
console.log(flavor == ('vanilla' || 'chocolate'));
and if I type instead:
var flavor = 'chocolate';
console.log(flavor == ('vanilla' && 'chocolate'));
it returns true. This doesn't make sense to me because logically, flavor cannot equal both ch...

1

votes

0

answer

68

Views

### Possible to have logical operations (e.g. `ndarray.__eq__`) not return `bool` in NumPy?

Question
What's the most elegant way to have NumPy not always force np.equal / np.ndarray.__eq__ return arrays of type bool, especially when dtype=object?
Problem
Project issue: https://github.com/RobotLocomotion/drake/issues/8315
We're presently using NumPy in a project, and we have symbolic scalar...

1

votes

1

answer

196

Views

### Non-numeric argument to binary operator, CSV

I've seen that other people before were already struggling with this, however I didn't manage to solve my problem with those posts. I get the error 'Non-numeric argument to binary operator'. The following reproducible example works:
x=rnorm(1000)+sin(c(1:1000)/100)#random data+ sinus superimposed
pa...

1

votes

2

answer

36

Views

### Selecting sub queries using all operator without joining in mysql

I need to display the ID, Name and Date of birth from all workers from department 1 who are younger than workers from department 2. I'm trying to do with without a join.
Here's what I have at the minute, it doesn't fail when I run it but it shows all the workers from department 2, regardless of thei...

1

votes

1

answer

77

Views

### Why Multiply operator does not work with BigInteger?

Can someone explain why the multiply operator throws an IllegalAccessException exception whereas all other operators works as expected?
BigInteger plus = 10000000000000000000 + 100 // 10000000000000000100
BigInteger minus = 10000000000000000000 - 100 // 9999999999999999900
BigInteger div = 100000000...

1

votes

0

answer

48

Views

### Drools Runtime does not recognize Custom Operator Parameter

Problem Statement:
We have few custom operators for Date and Strings, which are implemented looking at Str operator like shown here: https://github.com/kiegroup/drools/blob/f7ab4ee3c88d767a8cbb372c2db1afa84b20a51d/drools-core/src/main/java/org/drools/core/base/evaluators/StrEvaluatorDefinition.java...

1

votes

0

answer

48

Views

### Bitwise OR mask yields unexpected negative number

I am experimenting with the bitwise operators in javascript in an attempt to gain intuition. I want to input two 32 bit binary literals, OR them together, and output the result in a binary format. Taking the case of a 4-bit analog to my code below, I would expect that ORing together 0b1000 and 0b010...

66

votes

1

answer

163.5k

Views

### Using the && operator in an if statement

I have three variables:
VAR1='file1'
VAR2='file2'
VAR3='file3'
How to use and (&&) operator in if statement like this:
if [ -f $VAR1 && -f $VAR2 && -f $VAR3 ]
then ...
fi
When I write this code it gives error. What is the right way?

1

votes

2

answer

36

Views

### Comparison operators used for Boolean values [duplicate]

This question already has an answer here:
Python: frozensets comparison
1 answer
Python: See if one set contains another entirely?
7 answers
How do comparison operators work? I thought they could be used only to compare numeric values, 5

1

votes

0

answer

147

Views

### RxJS type issue in subscribe for pipeable operators

I am trying to use RxJS pipeable operators in my angular project. I have an implementation as below.
// Service Method
public getItems(params: Object = {}): Observable {
return this.apiService.get('v1/url', params);
}
// JsendResponse interface
import { Response } from '@angular/http';
export interf...

1

votes

0

answer

46

Views

### Alternative operators - causing errors in inline assembly

I'm working on a project, written in C++, that involves the injection of source code into an already compiled (and running) process, and so in a few cases I'm required to write inline assembly. This short introduction is to avoid any comment telling me inline assembly probably shouldn't be used.
So,...

1

votes

0

answer

218

Views

### Using C# System..Numerics.Vector<T> to unpack / pack bits

I’m testing the capabilities of the .Net C# System.Numerics.Vector class for packing and unpacking bits.
I was hoping for Vector bitwise shift left/right functionality but that is not currently available so I tried to simulate shifting using arithmetic & logical methods as below. Here’s what I s...

1

votes

1

answer

59

Views

### How to generate Roslyn Operator Overloads

I want to leverage Roslyn code generation to generate a Unary Operator and several Binary Operators but I was wondering how I could go about doing that.
Assumptions, Descriptor carries with it information such as the underlying TypeSyntax for which I am declaring the operators.
So far I've postulate...

1

votes

1

answer

33

Views

### Is the lost information by left shifting stored somewhere in Java? [duplicate]

This question already has an answer here:
Weird result of Java Integer left shift
2 answers
In Java, when using bitshifts, why does 1

1

votes

1

answer

27

Views

### The code generates the same combination twice.How can I improve it?

The code is as follows:
package AG;
import java.util.LinkedList;
import java.util.Stack;
public class ArithmeticGame {
public static void main(String[] args) {
LinkedList numbers = new LinkedList();
LinkedList number = new LinkedList();
for (int i = 1; i

1

votes

0

answer

42

Views

### What does the bitwise operator do before a function in Javascript? [duplicate]

This question already has an answer here:
What are the different ways of writing an IIFE? What are their use cases?
2 answers
What does the exclamation mark do before the function?
10 answers
I have seen the bitwise operator used in a self calling function, see below example, so I have a simple qu...

1

votes

1

answer

97

Views

### Bitwise shift vs Multiplication

I often use bitwise operators in my job and write on VB.NET.
When create variable type of System.Byte, it is required type cast to shift byte left:
Dim a As Byte = &HFF
Dim b As Integer = a * 256
Debug.WriteLine(b.ToString('X'))
Dim c As Integer = a

1

votes

2

answer

86

Views

### enum operators not working

My app has lots of bitfield enums that need various supporting functions (toString(), fromString(), countSetBits(), isValid(), etc…). So I used the CRTP to make a base class with static functions for everything I need. Everything works great, EXCEPT, I can’t get the bitwise operators to work....