Copyright HébergementWebs.com - License GPL

Tcl - Operators

Tcl tutorial   2020-11-21 20:27:05

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. The Tcl language is rich in built-in operators and provides the following types of operators - Arithmetic operators Relational operators Logical operators Bitwise operators Ternary operator This chapter will explain the arithmetic, relational, logical, bitwise and ternary operators one by one. Arithmetic operators The following table shows all the arithmetic operators supported by the Tcl language. Suppose the variable “A" contains 10 and the variable “B" has 20, then - Show examples Operator Deion Example + Add two operands A + B will give 30 - Subtract second operand from the first A - B will give -10 * Multiply the two operands A * B will give 200 / Divide the numerator by the denumerator B / A will give 2 % Module operator and the remainder after an B% A will give 0 Relational operators Next The table shows all the relational operators supported by the Tcl language. Suppose variable A contains 10 and variable B 20, then - Show examples Operator Deion Example == Checks whether the values ​​of two operands are equal or not, if so, the condition becomes true. (A = = B) is not true. ! = Checks whether the values ​​of two operands are equal or no, if the values ​​are not equal then the condition becomes true. (A! = B) is true. > Checks if the value of the left operand is greater than the value of the right operand, if so then the condition becomes true. (p> B) is not true. < Checks if the value of the left operand is less than the value of the right operand, if so, the condition becomes true. (A < B) is true. > = Checks if the value of the left operand is greater than or equal to the value of the right operand, if so then the condition dis true. (p> = B) is not true. <= Checks if the value of the left operand is less than or equal to the value of the right operand, if so, the condition becomes true. (A <= B) is true. Logical operators The following table shows all the logical operators supported by the Tcl language. Suppose variable A contains 1 and variable B contains 0, then - Show examples Operator Deion Example && Logical AND operator called. If the two operands are different from zero, the condition becomes true. (A && B) is false. || Logical OR operator called. If either of the two operands is nonzero, the condition becomes true. (A || B) is true. ! Logical operator NOT called. Use to invert the logical state of itsoperand. If a condition is true then the logical NOT operator will make it false. ! (A && B) is true. Bitwise operators The bitwise operator works on bitsand performs bitwise operations. The truth tables for &, | and ^ are as follows - p q p & q p | q p ^ q 0 0 0 0 0 0 1 0 1 1 1 1 1 1 0 1 0 0 1 1 Suppose that if A = 60; and B = 13; now in binary format they will be as follows - A = 0011 1100 B = 0000 1101 ----------- ----------- A&B = 0000 1100 A | B = 0011 1101 A ^ B = 0011 0001 The Bitwise operators supported by the Tcl language are listed in the following table. Suppose variable A contains 60 and variable B contains 13, then - Show examples Operator Deion Example & The binary AND operator copies the result a bit if it existsin both operands. (A & B) will give 12,which is 0000 1100 | The binary OR operator copies a bit if it existsin either operand. (A | B) give 61, which is 0011 1101 ^ L "Binary XOR operator copies the bit if it is set in an operand but not both. (A ^ B) will give 49, or 0011 0001 << Binary shift left operator. The value of the left operands is moved to the left by the number of bitsspecified by the right operand. A << 2 will give 240, or 1111 0000 >> Right binary shift operator. The value of the left operands is shifted to the right by the number of bitsspecified by the right operand. A >> 2 will give 15, which is 0000 1111 Ternair operatore Show examples Operator Deion Example ?: Ternary If the condition is true? Then value X: Otherwise value Y Priority of operators in Tcl The priority of operators determines the grouping of terms in an expression. This affectshow an expression is ev aluated. Some operators have a higher priority than others; for example, the multiplication operator has higher priority than the addition operator. For example : x = 7 + 3 * 2; here x is assigned to 13 and not to 20 because the * operator has a higher priority than +, so it is first multiplied by 3 * 2, then added to 7. Here, the operators with the highest priority appear at the top of the tablwater, those with the lowest appear at the bottom. In an expression, operators with higher precedence will be evaluated first. Show examples Category Operator Associativity Unary + - Right to left Multiplicative * /% From left to right Additive + - From left to right Shift <> From left right Relational << = >> = Left to right Bitwise AND & Left to right XOR bit by bit ^ Left to Right Bitwise OR | From left to right Logique AND && From left to right Logical OR || Left to right Ternary ?: Right to left