operators

Are & and <and> equivalent in python? [duplicate]

假如想象 提交于 2021-02-19 05:42:08
问题 This question already has answers here : 'and' (boolean) vs '&' (bitwise) - Why difference in behavior with lists vs numpy arrays? (8 answers) Closed 5 years ago . Is there any difference in the logic or performance of using the word and vs. the & symbol in Python? 回答1: and is a Boolean operator. It treats both arguments as Boolean values, returning the first if it's falsy, otherwise the second. Note that if the first is falsy, then the second argument isn't even computed at all, which is

Ruby && and = operators misudnerstanding

空扰寡人 提交于 2021-02-19 05:35:48
问题 What do you think would be the result of the next expression in Ruby? a = 10 && b = 25 Try to calculate in the ming and only then use irb . So, if we take a look at the Ruby documentation about Operators Precedence then we will se that && operator has a higher priority than = . So you must think that Ruby will evaluate the expression in the next way: a = ((10 && b) = 25) But Ruby does a job in another way: a = (10 && (b = 25)) # => 25 So, the priority of the = in b = 25 is higher, then && .

Deleting all lvalue conversion operators

馋奶兔 提交于 2021-02-18 12:55:08
问题 I want to avoid all lvalue conversions from a type to another type: struct A {}; struct T { A a; operator A() { return a; } //operator A&() = delete; how to delete lvalue conversions to A? }; void bar(A) {} void foo(const A&) {} void foo2(A&) {} int main() { T t; bar(t); // fine foo(t); // should never convert to ref-to-const A foo2(t); // should never convert to ref-to A return 0; } Is this possible? How and which conversion operators do I need to delete? Example on godbolt 回答1: You might do

Odd and Even numbers (using & or %)

╄→гoц情女王★ 提交于 2021-02-18 12:14:06
问题 I've always used the following in order to find even and odd numbers: if( $num % 2 ) { echo "odd"; } if( !($num % 2) ) { echo "even"; } But recently I stumbled upon with the following code that works exactly the same: if( $num & 1 ) { echo "odd"; } if( !($num & 1) ) { echo "even; } What's the logic behind the "&" in the second method? I went to check the PHP: Arithmetic Operators and the ampersand is not part of the options. Thanks. 回答1: It is the bitwise-AND operator. Remember that in the

Does the Java &= operator apply & or &&?

浪子不回头ぞ 提交于 2021-02-17 07:48:27
问题 Assuming boolean a = false; I was wondering if doing: a &= b; is equivalent to a = a && b; //logical AND, a is false hence b is not evaluated. or on the other hand it means a = a & b; //Bitwise AND. Both a and b are evaluated. 回答1: From the Java Language Specification - 15.26.2 Compound Assignment Operators. A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)) , where T is the type of E1 , except that E1 is evaluated only once. So a &= b; is

Operators = vs. == in PHP

和自甴很熟 提交于 2021-02-17 06:54:07
问题 I am learning basic PHP from a book, and from what I read, = is an assignment operator, and == is a comparison operator. So... $x = 5; $x == 5: true ...makes sense. However, the book gives an example which confuses me: if (++$x == 10) echo $x; Why ==? Aren't we trying to say "if ++$x equals 10, then echo $x"...? Then that would seem like: if (++$x = 10). The former would be like asking a question inside a conditional statement, which would be illogical or redundant. 回答1: == means equality, so

Why can't I dereference a pointer to an object that's an array-element using the indirection operator?

百般思念 提交于 2021-02-16 09:48:53
问题 Is it not possible to dereference a pointer to an object that's stored in an array using the indirection(dereference) operator or am I doing something wrong? #include <iostream> class A { public: virtual void test() { std::cout << "A\n"; } }; class B : public A { public: void test() { std::cout << "B\n"; } }; int main() { A* v[2]; v[0] = new A(); v[1] = new B(); v[0]->test(); *(v[1]).test(); // Error! If the arrow operator is used instead // though, the code compiles without a problem. return

Understanding the use of operator () (Function Object)

余生颓废 提交于 2021-02-11 14:25:11
问题 In this code below I can´t understand really well how can std::generate(v2.begin(), v2.end(), r) call the rand() if there isn´t the operator () in r or even any parameters on it. class RandomInt { public: RandomInt(int a, int b); int operator()(); private: int limInf, limSup; // interval limits: [limInf..limSup] }; //---------------------------------------------------------------------- RandomInt::RandomInt(int a, int b) { limInf = a; limSup = b; } //------------------------------------------

What are built-in Python 3 types that can be compared to each other?

北城以北 提交于 2021-02-10 14:40:59
问题 In Python 2, it was possible to compare objects of different types such as int to str by having an implicit comparison of the text string of types (that is, in lexicographic order, string 'int' is less than string 'str' and string 'list' is less than string 'tuple' ). Hence, in Python 2, 5 < 'hello' returns True . One can read more about why this was allowed in answer to Why is ''>0 True in Python?. In Python 3, this raises builtins.TypeError: unorderable types: int() < str() exception. This

What are built-in Python 3 types that can be compared to each other?

别来无恙 提交于 2021-02-10 14:37:04
问题 In Python 2, it was possible to compare objects of different types such as int to str by having an implicit comparison of the text string of types (that is, in lexicographic order, string 'int' is less than string 'str' and string 'list' is less than string 'tuple' ). Hence, in Python 2, 5 < 'hello' returns True . One can read more about why this was allowed in answer to Why is ''>0 True in Python?. In Python 3, this raises builtins.TypeError: unorderable types: int() < str() exception. This