beautypg.com

Measurement Computing LogBook Series User Manual

Page 165

background image

Software Reference

989592

LogView LV-39

Bitwise Operators

The bitwise operators perform bitwise-AND (&), bitwise-OR (

|

), and bitwise-Not (

~

) operations.

Syntax

AND-expression & equality-expression
OR-expression | Not-expression
Not-expression

~

AND-expression

The operands of bitwise operators must have integral types, but their types can be different. These
operators perform the usual arithmetic conversions; the type of the result is the type of the operands after
conversion.

&

The bitwise-AND operator compares each bit of its first operand to the corresponding bit of its second

operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result
bit is set to 0.
Example: 10110000

&

10010000 = = 10010000

|

The bitwise-OR operator compares each bit of its first operand to the corresponding bit of its second
operand. The operator is inclusive in that, if either bit is 1, the corresponding result bit is set to 1.
Otherwise, the corresponding result bit is set to 0.
Example: 10110000

|

10010000 = = 10110000

~

The bitwise-NOT operator creates a bitwise compliment of its operand. Thus, a 0 switches to 1, and a

1 switches to 0.
Example:

~

10110000 = = 01001111

Bitwise Left Shift and Right Shift Operators:

<<

,

>>

Syntax:

shift-expression << additive-expression
shift-expression >> additive-expression

The bitwise shift operators shift their first operand left (<<) or right (>>) by the number of positions the
second operand specifies.

Example: 10110000 << 2 = = 11000000

Logical Operators

The logical operators perform logical AND, logical OR, and logical NOT operations.

Logical operators do not perform usual arithmetic conversions. Instead, they evaluate each operand in
terms of its equivalence to 0. Thus, the result of a logical operation is either 0 or 1.

AND The logical-AND operator produces the value 1 if both operands have nonzero values. If either

operand is equal to 0, the result is 0. If the first operand of a logical-AND operation is equal to 0,
the second operand is not evaluated.

OR

The logical-OR operator performs an inclusive-OR operation on its operands. The result is 0 if
both operands have 0 values. If either operand has a nonzero value, the result is 1. If the first
operand of a logical-OR operation has a nonzero value, the second operand is not evaluated.

The operands of logical-AND and logical-OR expressions are evaluated from left to right. If the
value of the first operand is sufficient to determine the result of the operation, the second operand
is not evaluated. This is called "short-circuit evaluation."

NOT The logical-negation (logical-NOT) operator produces the value 0 if its operand is true (nonzero)

and the value 1 if its operand is false (0). The operand must be an integral, floating, or pointer
value.