## Wednesday, April 23, 2008

### Smooth Operators

Relational operators.

Relational operators are used indecision making. There are 6 different type of operator at your disposal. They are:-

 Op Comment == is equal to != not equal to < less than <= less than OR equals to > more than >= more than OR equals to

These operators are used in loops, if(), while, for() or where ever is decision needs to be made. Take care when using this. Example if you want the loop to perform 4 cycles, writing a for loop for example:

for (x=1; x<4; x =4;x++); This only preform 3 iterations (loops), because when x=4, the condition will be false. 4<4, No 4 is equal to 4. So to overcome this problem, use x<=4 or x<5. This will achieve the result you want of 4 loops

Bit operators

 & AND | OR ~ NOT ^ XOR >> Shift right << Shift left

How this works is as follows, variables alpha, bravo and result used.

alpha=0b10101010
bravo=0b11000110
result=0;

AND
result=alpha & bravo;

10101010 alpha
11000110 bravo
-------------
10000010 result
========

Only when BOTH values are 1 is the result 1. Similar to multiplication.

OR
result = alpha | bravo;

10101010 alpha
11000110 bravo
------------
11101110 result
=======

When EITHER values is 1, is the result 1. Similar to addition.

NOT
result= ~alpha;

10101010 alpha
------------
01010101 result
=======

When bit is 1 then result 0, when bit is 0 result 1.

XOR (exclusive OR)
result=alpha ^ bravo;

10101010 alpha
11000110 bravo
------------
01101100 result
========

When BOTH values 1 OR, BOTH values 0, result 0. When either value 1 AND 0, result 1.

Shift right
result=alpha >> 2;

10101010 alpha
------------
00101010 result
========

Moves 2 bits to right and replaces left most bits with 0.

Shift left.

result=alpha << 3

10101010 alpha
-----------
01010000 result
=======

Moves 3 bits to left and replaces right most bits with 0.

Boolean operators.

There will come a time, you will need to check on more and one value to make a decision. In your program you could do this:-

if (portb.f4==1) {
if (portc.f2==1) {
.
do processing.
.
.
}
}

This code will check if portb.f4=1 then check if portc.f2 = 1 then process the lines below it.

Another way of doing this will be by using a Boolean operator.

if (portb.4==1 && portc.f2 ==1) {

do processing1;

}

else {

do processing2;

}

The && is a Boolean AND

 || OR TRUE when EITHER condition is true && AND TRUE when BOTH/ALL condition true ! NOT TRUE when FALSE

There are 3 Boolean operators, they are:

Using OR

if(var1==3 || var2 ==4 || varN==”CLS”) {

If either condition is true, process code below the if.

Using AND

if(var1==3 && var2==4 && varN==”CLS”) {

Only when all condition are true, process code below the if.

Using NOT

if (!EOF) {

if not end of file process code below if.

Assignment Operator

The single equal sign (=) is an assignment operator and assigns a value to a variable or constant (aka lvalues) AND is not a and used for comparison. Common mistake.

Ex.

if(portb==128) ….checks if portb is equal to 128

portb = 128; … assigns the value 128 to portb.

name[] = “9w2gu”

error = 255

const int ms=50;

Math operators

We all know how to do math right? We all know what + - * / % means.

But wait! What is * / %, not in my math book!

* is multiplication

/ is division

% modulus (remainder value)

Now you want to perform calculation on 3 numbers.

3 * 2 + 1;

Though not wrong (but not the answer you wanted), and answer will be 7, but the () should be used to group the calculation. (3 * 2) + 1 = 3 * 2 +1.

If I type 1 * 2 + 3 the answer now will be 6. This is because multiplication has a higher precedence than addition. So if you wanted to add then multiply,

then 3 * (2 + 1) = 9. If there are many ( ) within a formula, the inner most group of ( ) will be calculated first.

Ex.

result=2*(24/x-(3*y))

(3*y) is calculated first then 24/x. Then the subtraction and last multiplied by 2.