tailieunhanh - C 2.0 practical guide for programmers PHẦN 5

Cho a, b là các giá trị bit tương ứng trong các toán hạng bên trái và bên phải, tương ứng, khai thác hợp lý (bitwise) được định nghĩa như trong Bảng . Đảo ngược giá trị bit (Bổ sung) 1 nếu cả hai bit là 1, nếu không 0 1 nếu một trong hai hoặc cả hai bit là 1, nếu không 0 1 nếu và chỉ nếu một trong các bit là 1, khác 0 | Logical Operators 89 t b where f not evaluated short-circuited 0 t b f f b where t not evaluated short-circuited 0 f t b f f where b not evaluated short-circuited 0 f f b t b where f not evaluated short-circuited 0 f t b Output True False False False True True True False True True False True Logical Operators Given that a and b are corresponding bit values in the left-hand and right-hand operands respectively the logical bitwise operators are defined as in Table . Name Notation Meaning Logical NOT a Logical AND a b Logical OR a b Logical XOR a b Invert the bit value Complement 1 if both bits are 1 otherwise 0 1 if either or both bits are 1 otherwise 0 1 if and only if one of the bits is 1 otherwise 0 Table Logical operators. a b a a b a b a b 1 1 0 1 1 0 1 0 0 0 1 1 0 1 1 0 1 1 0 0 1 0 0 0 Table Values for logical operators. 90 Chapter 5 Operators Assignments and Expressions Valid types for logical operators are integers and boolean. Example public class LogicalBitwise public static void Main string args ushort a 0X005A in binary 0000 0000 0101 1010 ushort b 0x3C5A in binary 0011 1100 0101 1010 0 x a b 0 x a b 0 x a b 0 x a 0 x b Output 5a 3c5a 3c00 ffffffa5 ffffc3a5 Logical Operators as Conditional Logical Operators The logical bitwise operators may also be used as conditional logical operators since they can be applied to boolean operands and return a bool value. Given that a and b represent boolean expressions the logical operators are defined as in Table . Name Notation Meaning Logical NOT a Logical AND a b Logical OR a b Logical XOR a b Returns the complement of the truth value of a negation true if both operands are true otherwise false true if either or both operands are true otherwise false true if and only if one operand is true otherwise false .

TỪ KHÓA LIÊN QUAN