Why Would You Use a !! Operator

Why would you use a !! operator

You use it if you only want the boolean, not the object. Any non-nil object except for boolean false represents true, however, you'd return the data as well. By double negating it, you return a proper boolean.

Why would I want a .* operator in C++?

Your example is too trivial to be illustrative. Consider a bit more complicated one

struct A {
int a;
int b;
};

void set_n_members(A objs[], unsigned n, int A::* ptr, int val)
{
for (unsigned i = 0; i < n; ++i)
objs[i].*ptr = val;
}

int main()
{
A objs[100];
set_n_members(objs, 100, &A::b, 5);
set_n_members(objs, 100, &A::a, 7);
}

How would you rewrite this without int A::* ptr and without inducing code bloat?

When to use ^ operator

^ is a Logical XOR Operator if the operands are bools, otherwise it's a Bitwise XOR Operator

Binary ^ operators are predefined for the integral types and bool. For integral types, ^ computes the bitwise exclusive-OR of its operands. For bool operands, ^ computes the logical exclusive-or of its operands; that is, the result is true if and only if exactly one of its operands is true.

http://msdn.microsoft.com/en-us/library/zkacc7k1.aspx

Why do we need operator functions in python?

As an example of why you might like to be able to call an operator as a function, consider the following code:

    if op == "+":
return num1 + num2
elif op == "-":
return num1 - num2
elif op == "*":
return num1 * num2
else:
raise ValueError(f"invalid operator {op}")

With operator this can be written more easily as:

    return {
"+": operator.add,
"-": operator.sub,
"*": operator.mul,
}[op](num1, num2)

Understanding the is operator

You misunderstood what the is operator tests. It tests if two variables point the same object, not if two variables have the same value.

From the documentation for the is operator:

The operators is and is not test for object identity: x is y is true if and only if x and y are the same object.

Use the == operator instead:

print(x == y)

This prints True. x and y are two separate lists:

x[0] = 4
print(y) # prints [1, 2, 3]
print(x == y) # prints False

If you use the id() function you'll see that x and y have different identifiers:

>>> id(x)
4401064560
>>> id(y)
4401098192

but if you were to assign y to x then both point to the same object:

>>> x = y
>>> id(x)
4401064560
>>> id(y)
4401064560
>>> x is y
True

and is shows both are the same object, it returns True.

Remember that in Python, names are just labels referencing values; you can have multiple names point to the same object. is tells you if two names point to one and the same object. == tells you if two names refer to objects that have the same value.

Why would one use the |= operator on a boolean value in C#?

For no good reason at all. A boolean value |= true will always be true. This is someone trying to be fancy, or forgetting boolean logic =)

Change it to tmp = true;.

What does the += operator do in Java?

The "common knowledge" of programming is that x += y is an equivalent shorthand notation of x = x + y. As long as x and y are of the same type (for example, both are ints), you may consider the two statements equivalent.

However, in Java, x += y is not identical to x = x + y in general.

If x and y are of different types, the behavior of the two statements differs due to the rules of the language. For example, let's have x == 0 (int) and y == 1.1 (double):

    int x = 0;
x += 1.1; // just fine; hidden cast, x == 1 after assignment
x = x + 1.1; // won't compile! 'cannot convert from double to int'

+= performs an implicit cast, whereas for + you need to explicitly cast the second operand, otherwise you'd get a compiler error.

Quote from Joshua Bloch's Java Puzzlers:

(...) compound assignment expressions automatically cast the result of
the computation they perform to the type of the variable on their
left-hand side. If the type of the result is identical to the type of
the variable, the cast has no effect. If, however, the type of the
result is wider than that of the variable, the compound
assignment operator performs a silent narrowing primitive
conversion [JLS 5.1.3].



Related Topics



Leave a reply



Submit