Lesson 5: Operators and Integer operations


E-mail this post



Remember me (?)



All personal information that you provide here will be governed by the Privacy Policy of Blogger.com. More...



Again, I'm working as fast as I can on development of new C material, but also am cautious to avoid mistakes. If you encounter any, feel free to add comment so I can remove them. Here's fifth lesson on operators, their priority and laws of casting (transforming) data types in automatic (implicit) or given (explicit) way. Hope you have no problem following my text, I include as much examples I can to help you visualize and understand C++ programming language, eventhough these are C foundations for now. Here we go...



DIVIDING INTEGERS

It’s important to notice that sometimes we get “unwanted” results when dividing whole numbers. For example if we want to assign ½ to real variable a, assigning won’t work as we would like it; the result wouldn’t be 0.5:


a = 1 / 2;


In this expression, right side of equation consist of two whole number operands, thus the dividing result will be whole number. Result will be 0 (with remainder 1).

To avoid this kind of unwanted results, its smart to use explicit casting (given transformation) of data types mentioned in Lesson 4 (its enough to use it only once on one operand) or to set constants so at least one operand is real type:


a = (float) 1 / 2; or

a = 1. / 2; or

a = 1 / 2.;


Important:

First case shows us way of using explicit casting (it could also be used on second operand), and second and third example shows us possibility of adding spot, which declares constant as real number. This makes our result to be processed in real domain.



OPERATORS PRIORITY


1. * / %

2. + -


If the expression has more operators of equal priority, they process in a priority from left to right, one after another. Expressions inside ( ) have highest priority.


Example:

What’s the result of following expression?

5 + 10 / 3 * ( 8 – 6 )

5 + 10 / 3 * 2

5 + 3 * 2

5 + 6

11


Example:

What’s the result?

a) 9 / 4

Result: 2 (dividing of whole numbers)


b) 9 % 4

Result: 1 (% means remainder form division of whole numbers: 9 : 4 = 2 + remainder 1)


Example:

What’s the result we get assigned to following operands i, x, c


int i;

double x, c, d;

after these phrases:

d = 6.0;

i = (int)(d + 1.73);

x = i / 2;

c = (double)i / 2;


Result:

i = 7, x = 3, c = 3.5



OPERATORS REFLECTION ON BITS


& AND

| OR

^ XOR

<< SHIFT LEFT

>> SHIFT RIGHT

~ NOT


Operators &, |, ^ are binary operators (defined above two bits). Operator ~ is unary operator (defined above single bit).



Operators reflecting on bit operands (b1 and b2 represent bits):

b1

b2

b1 & b2

b1 | b2

b1 ^ b2

0

0

0

0

0

0

1

0

1

1

1

0

0

1

1

1

1

1

1

0



b1

~b1

0

1

1

0


Example:

Calculate phrases: 3 & 5, 3 | 5, 3 ^ 5, ~3


  0000 0011 (3)
&0000 0101 (5)
  -------------------

0000 0001 (1)



  0000 0011 (3)
| 0000 0101 (5)
  -------------------

0000 0111 (7)



  0000 0011 (3)
^ 0000 0101 (5)
  -------------------

0000 0110 (6)



~ 0000 0011 (3)

--------------------

1111 1100 (252 or –4)



Operators <<>> are used to reposition all the variables content bits, left or right. Repositioning all the bits one place left is the same as multiplying the variables content by 2 (x2), while repositioning one place right is the same as dividing content with 2 (/2).

Electronic computers in general have repositioning instruction implemented in their registry and in this way multiplying or dividing using number proportional with 2, is many times faster than classical multiplying or dividing.

Number of places this operand repositions the content, is given with parameter.


Example:

Calculate these phrases: 2<<1 , and 37 >>2


0000 0010 (2)

After reposition one place left

0000 0100 (4)


0010 0101 (37)

After reposition two places right (37>>2), result is variable divided with 4 (whole number part):

0000 1001 (9)


Important:

In case data is saved in a single octet, without the bit reserved for foresign (+/-), calculate 128 <<1


1000 0000 (128)

After reposition one place left:

0000 0000 (0)


In case data is saved in single octet, including the bit reserved for foresign (+/-), calculate 64 <<1

0100 0000 (64)

After reposition one place left:

1000 0000 (-128)



Technorati Tags: , , , , , ,



6 Responses to “Lesson 5: Operators and Integer operations”

  1. Anonymous Anonymous 

    with last two examples i had trouble inserting right code for calculation:

    so read them as 128 << 1 instead of 128 <<>

    and next one 64 << 1 instead if 64 <<1>

  2. Anonymous Anonymous 

    Great job so far. I only wish you'd downgrade your pace to a more steady one, to keep up to the task in the long run.

  3. Anonymous Anonymous 

    hy

    didn't understand what does "downgrade your pace to a more stady one" mean, please explain.. thnks! if you mean if I'll keep posting don't worry, I'll publish materials unitll I cover all topics on programming with c

    I appreciate your comments all the time!

  4. Anonymous Anonymous 

    Exactly, you got it right.

  5. Anonymous Anonymous 

    Good work

  6. Anonymous Anonymous 

    This is the second tutorial I have started to read but you are already getting into some difficult stuff but you have not even talked about 'int main', '#include' or even the syntax of a c file. The examples and tutorials are pretty good if you already know how to program in C (as far as basics anyways) but to a beginner, they would be very confused!

    -Black Jack-

Leave a Reply

      Convert to boldConvert to italicConvert to link

 


German Flag Spanish Flag French Flag Italian Flag Portuguese Flag Japanese Flag Korean Flag Chinese Flag British Flag


This Website is optimized for Firefox. Users browsing with Internet Explorer may encounter problems while viewing pages.


C++ Maniac



Learn C



Additional



#include



Learn Converting



Appendix


Links


Previous posts




Daily Lessons for programming in Visual Studio, using C code.