unary-operator

Unary operator ++ cannot be applied to an operand of type Int

不羁的心 提交于 2019-12-10 14:28:22
问题 Why does the following swift code bring me the error "Unary operator '++' cannot be applied to an operand of type 'Int'" ??? (using swift-1.2 on Xcode-6.3.2) struct Set { var player1Games: Int var player2Games: Int init() { self.player1Games = 0 self.player2Games = 0 } func increasePlayer1GameScore () { player1Games++ // error: Unary operator '++' cannot be applied to an operand of type 'Int' } func increasePlayer2GameScore () { player2Games++ // error: Unary operator '++' cannot be applied

Why does F# have a unary plus operator?

我的梦境 提交于 2019-12-10 12:35:19
问题 Some languages use a unary plus operator for implicit conversions, such as coercing a string to a number (e.g. Javascript) or casting small number types to an int (e.g. most C-based languages), or to be used when overloading operators. Since the unary plus is primarily used for hackish purposes like this, and also since F# does not perform automatic widening conversions, I was surprised that F# includes the unary plus. What adds to my surprise is that Haskell does not have a unary plus

Does Unary + operator do type conversions?

爱⌒轻易说出口 提交于 2019-12-09 00:30:32
问题 Till now I was believing that there is no use of unary + operator. But then I came across with following example: char ch; short sh; int i; printf("%d %d %d",sizeof(ch),sizeof(sh),sizeof(i)); // output: 1 2 4 printf("%d %d %d",sizeof(+ch),sizeof(+sh),sizeof(i)); // output: 4 4 4 Does it mean + is doing type conversion here? Because it is behaving same as following printf("%d %d %d",sizeof((int)ch),sizeof((int)sh),sizeof(i)); // output: 4 4 4 This forces me to think + is doing type conversion.

Why do the plus and unary plus behave strange in array syntax?

余生长醉 提交于 2019-12-08 19:10:03
问题 Following this question on the plus operator I have a follow-up question. We know the difference between plus and uplus, and thus that 1+2 resolves to 3 , just as 1++2 or even 1++++++++2 . The strange thing happens in array syntax, consider this example: >> [1 ++ 2] ans = 1 2 % Two unary plusses >> [1 + + 2] ans = 3 % A normal plus and a unary one >> [1++2] ans = 3 % A normal plus and a unary one The same works with multiple plusses, [1 +++..+++ 2] , so with all plusses consecutively in the

In C, how to get calculate the negative of an unsigned quantity

被刻印的时光 ゝ 提交于 2019-12-07 02:40:40
问题 In K&R ANSI C book, section A.7.4.5 (Unary Minus Operator) it is stated: ... The negative of an unsigned quantity is computed by subtracting the promoted value from the largest value of the promoted type and adding one; ... How exactly is this calculated? Could you give a short C example? I don't see how this could yield the negative of, say, 200u: subtracting 200 from a max value of any integral type (signed or unsigned) and adding 1 does not result in -200. I know what the Unary minus does

Scala - Prefix Unary Operators

假如想象 提交于 2019-12-06 19:50:45
问题 I've recently given Scala a second chance, and started with the project I always implement (in functional or pseudo-functional languages): an automated reasoner for propositional logic (and later predicate logic). Now, I've tried to get the notation of propositional logic in the language itself as pretty as possible, and I've gotten this far - with an implicit conversion (String -> Atom): ("A" and "B") implies "C" The functions "and" and "implies" (and "or" and "equivalent") are simple

unary minus for 0x80000000 (signed and unsigned)

China☆狼群 提交于 2019-12-06 13:40:20
The n3337.pdf draft, 5.3.1.8, states that: The operand of the unary - operator shall have arithmetic or unscoped enumeration type and the result is the negation of its operand. Integral promotion is performed on integral or enumeration operands. The negative of an unsigned quantity is computed by subtracting its value from 2ⁿ, where n is the number of bits in the promoted operand. The type of the result is the type of the promoted operand. For some cases it is enough. Suppose unsigned int is 32 bits wide, then (-(0x80000000u)) == 0x80000000u , isn't it? Still, I can not find anything about

Operator precedence or Maximal Munch Rule comes first for Unary Operators

我与影子孤独终老i 提交于 2019-12-06 05:16:45
问题 Here I am having the following piece of code: int a,b,x; a=b=1; x=a+++b; Now the value of x will be 2 as a is first being post incremented and then it is being added to b . Following is the compiled byte code : 0 iconst_1 1 dup 2 istore_2 [b] 3 istore_1 [a] 4 iload_1 [a] 5 iinc 1 1 [a] 8 iload_2 [b] 9 iadd 10 istore_3 [x] So the expression will be equivalent to x = (a++) + b . Now the other expression x=a++++b , won't compile because of the maximal munch rule . It will become x = (a++) ++ b

Multiple unary operators in an if statement [duplicate]

橙三吉。 提交于 2019-12-05 03:46:53
This question already has an answer here: Using the && operator in an if statement 1 answer Is it possible to have multiple unary operators in if statements.. Here is the code snippet which is giving me error. Please correct the code here. if [ -f $input_file ] -a [ -f $output_file ] -a [ -f $log_file ] ] then ### Some Code here fi if [ -f "file1" -a -f "file2" -a "file3" ]; then #some code fi If you use Bash's double-bracket, you can do this: if [[ -f "$input_file" && -f "$output_file" && -f "$log_file" ]] which I find cleaner to read than other options shown here (but that's subjective).

Operator precedence of unary operators

我的未来我决定 提交于 2019-12-04 22:12:32
问题 Some information source on operator precedence like this says that unary operators like ! , ~ , + , - have higher precedence than assignment = . However, the following expressions are possible: !a = true # => false (with warning) a # => true ~a = 1 # => -2 a # => 1 +a = 1 # => 1 a # => 1 -a = 1 # => -1 a # => 1 Considering these results, the only possible explanation I can think of is that these unary operator have lower precedence than the assignment. If that is the case, then it would mean