Page 37 - DCAP404 _Object Oriented Programming
P. 37
Object-oriented Programming
Notes 10/23, which is equivalent to 1.
When you do a left shift by 3 (10 << 3), the result is
0 1 0 1 0 0 0 0
10*23, which is equivalent to 80
Shifting Negative Numbers
For negative numbers, the unused bits are initialized to 1. Therefore, –10 is represented as:
1 1 1 1 0 1 1 0
2.3.6 Bit-wise Operators
Operator Description Example Explanation
& Evaluates to a binary a & b AND results in a 1 if both
(AND) value after a bit-wise the bits are 1, any other
AND on the operands combination results in a 0
! Evaluates to binary a ! b OR results in a 0 when
(OR) value after a both the bit-wise OR on
two operands the bits are 0, any other
combination results in a 1.
^ Evaluates to a binary a ^ b XOR results in a 0 if both
(XOR) value after a bit-wise the bits are of the same
XOR on the two operands value and 1 if the bits have
different values.
~ Converts all 1 bits to 0s and Example given below.
(inversion) all 0 bits to 1s
In the example shown in the table, a and b are integers and can be replaced with expressions that
give a true or false (boo1) result.
Example: When both the expressions evaluate to true, the result of using the & operator
is true. Otherwise, the result is false.
The ~ Operator
If you use the ~ operator, all the 1a in the bite are converted to 0s and vice versa. For example,
10011001 would become 01100110.
2.3.7 Logical Operators
Use logical operators to combine the results of Boolean expressions.
Operator Description Example Explanation
&& Evaluates to true, if a>6&&y<20 The result is true if
both the conditions evaluate condition 1 (a>6) and
to true, false otherwise condition 2 (y<20) are
both true. If one of them is
false, the result is false.
30 LOVELY PROFESSIONAL UNIVERSITY