home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Reference Guide / C-C++ Interactive Reference Guide.iso / c_ref / clesson / clesson.exe / clesson.DXR / 00029.txt < prev    next >
Encoding:
Text File  |  1995-04-04  |  9.5 KB  |  295 lines

  1.  
  2.                                                   Lesson 4
  3.  
  4.                                                                          
  5.                                 The operators of the language.
  6.  
  7.         I have mentioned that 'C' is a small language with most of the heavy
  8. work being done by explicit calls to library functions. There is however a rich mix of intrinsic operators which allow you to perform bit level operations, use pointers, and perform immediate operations on varables. In other words, most of a machine's instruction set is able to be used in the object program.
  9. At the time when 'C' was designed and first written these were unique for
  10. a high level language.
  11.  
  12.   Lets start with a discussion about precedence.
  13.  
  14.  
  15.  
  16.         This really means that the compiler puts invisable parentheses into
  17. your expression. Casting your mind back to Arithmetic in the primary school I expect you remember the nmemonic "My Dear Aunt Sally". The 'C' language does as well! So the following expression is correct
  18.  
  19.         15 + 4 * 11 = 59
  20.  
  21.         The compiler has rendered the expression as:
  22.  
  23.         15 + ( 4 * 11 ) = 59
  24.  
  25.         Now the 'C' language has a much larger collection of operators than
  26. just Multiply Divide Add Subtract, in fact much too big to try to remember theprecedence of all of them. So my recomendation is to ALWAYS put in the parentheses, except for simple arithmetic. However, for the sake of
  27. completeness as much as anything else, here is the list.
  28.  
  29.  
  30.  
  31.         First up come what are called the primary-expression operators:
  32.  
  33.                 ()    Function.
  34.                 []    Array.
  35.                 .     struct member ( variable ).
  36.                 ->    struct member ( pointer ).
  37.  
  38.          The unary operators:
  39.  
  40.                 *     Indirection via a Pointer.
  41.                 &     Address of Variable.
  42.                 -     Arithmetic Negative.
  43.                 !     Logical Negation or Not.
  44.                 ~     Bit-wise One's Complement.
  45.                 ++    Increment.
  46.                 --    Decrement.
  47.                 sizeof  Which is self explanitary.
  48.  
  49.  
  50.         Now the binary operators:
  51.  
  52.    Arithmetic Operators.
  53.  
  54.                 *     Multiply.                                       My
  55.                 /     Divide.                                         Dear
  56.                 %     Modulo, or Remainder of Integer Division.
  57.                 +     Addition.                                       Aunt
  58.                 -     Subtraction.                                    Sally
  59.  
  60.          The Shifting Operators.
  61.  
  62.                 >>    Bit-wise Shift to the Right.
  63.                 <<    Bit-wise Shift to the Left.
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.    Logical Relation Operators.
  71.  
  72.                 <     Less Than.
  73.                 >     Greater Than.
  74.                 <=    Less Than or Equal.
  75.                 >=    Greater Than or Equal.
  76.                 ==    Equal.
  77.                 !=    Not Equal.
  78.  
  79.          Bit-wise Boolean Operators.
  80.  
  81.                 &     Bit-wise And.
  82.                 ^     Bit-wise Exclusive-or.
  83.                 |     Bit-wise Or.
  84.  
  85.  
  86.  
  87.  
  88.  
  89.          The Logical Operators.
  90.  
  91.                 &&    Logical And.
  92.                 ||    Logical Or.
  93.  
  94.    The Assignment Operators. ( They all have the same priority. )
  95.  
  96.                 =     The normal assignment operator.
  97.  
  98.          The Self-referencing Assignment Operators.
  99.  
  100.                 +=
  101.                 -=
  102.                 *=
  103.                 /=
  104.                 %=
  105.                 >>=
  106.                 <<=
  107.                 &=
  108.  
  109.  
  110.                 ^=
  111.                 |=
  112.  
  113.   Some explanation is in order here. The machine instructions in your
  114. computer include a suit of what are called "immediate operand" instructions.
  115. These instructions have one of the operands in a register and the other
  116. is either part of the instruction word itself ( if it is numerically small
  117. enough to fit ) or is the next word in the address space "immediately" after
  118. the instruction code word. 'C' makes efficient use of this machine feature
  119. by providing the above set of operations each of which translates directly
  120. to its corresponding machine instruction. When the variable in question is a
  121. 'register' one, or the optimiser is in use, the compiler output is just
  122. the one "immediate" machine instruction. Efficiency Personified!!!
  123.  
  124.         These two lines will make things clearer.
  125.  
  126.  
  127.  
  128.  
  129.         a = 8;
  130.         a += 2;     /* The result is 10 */
  131.  
  132.         The exclusive-or operation is very useful you can toggle any
  133. combination of bits in the variable using it.
  134.  
  135.         a = 7;
  136.         a ^= 2;    /* Now a is 5 */
  137.         a ^= 2;    /*  and back to 7. */
  138.  
  139.         Naturally, you can use the other operations in exactly the same way,
  140. I'd like to suggest that you make a utterly simplistic little program
  141. and have a look at the assembler code output of the compiler. Don't be
  142. afraid of the assembler codes - they don't bite - and you will see
  143. what I was on about in the paragraph above.
  144.  
  145.         Historical Note and a couple of Cautions.
  146.  
  147.  
  148.  
  149.         In the Oldend Days when 'C' was first written all the self-referencing
  150. operations had the equals symbol and the operand around the other way.
  151. Until quite recently ( unix system V release 3.0 ) the 'C' compiler had a
  152. compatability mode and could cope with the old style syntax.
  153.  
  154.         A sample or test program is probably in order here.
  155.  
  156. /* ----------------------------------------- */
  157.  
  158. #include <stdio.h>
  159.  
  160. char *mes[ ] =
  161. {
  162.  
  163.  
  164.  
  165.  
  166.  
  167.         "Your compiler",
  168.         " understands",
  169.         " does not understand",
  170.         " the old-fashioned self-referencing style."
  171.         };
  172.  
  173. main()
  174. {
  175.         int a;
  176.  
  177.         a = 5;
  178.         a=-2;
  179.         printf ( "%s %s %s\n", mes [ 0 ], mes [ ( a == -2 ) ? 2 : 1 ], mes [ 3
  180. ] );
  181.         }
  182.  
  183. /* ----------------------------------------- */
  184.  
  185.  
  186.  
  187.         The 'C' compiler issued with unix System V release 3.2 seems to have ( thankfully ) dropped the compatability mode. However a collegue, who was using an old compiler, and I spent hours trying to find this strange bug!
  188. The cure for the problem is either to put spaces on either side of the '=' sign or to bracket the unary minus to the operand.
  189.  
  190.         a=(-2);
  191.         a = -2;
  192.  
  193. Either is acceptable, and might save you a lot of spleen if sombody tries
  194. to install your work of art program on an ancient machine.
  195.  
  196.   The other caution is the use of the shifting instructions with signed
  197. and unsigned integers.
  198.  
  199.    
  200.  
  201.      If you shift a signed integer to the right when the sign bit is set
  202. then in all probability the sign will be extended. Once again a little
  203. demo program. Please cut it out of the news file with your editor
  204. and play with it.
  205.  
  206. /* ----------------------------------------- */
  207.  
  208. #ident "#(@) shifts.c - Signed / Unsigned integer shifting demo."
  209. #include <stdio.h>
  210.  
  211. #define WORD_SIZE ( sizeof ( INTEGER int ) * 8 )
  212. #define NIBBLE_SIZE 4
  213. #define NIBBLES_IN_WORD (( WORD_SIZE ) / NIBBLE_SIZE )
  214. #define SIGN_BIT ( 1 << ( WORD_SIZE - 1 ))
  215.  
  216.  
  217.  
  218.  
  219. char *title[ ] =
  220. { "       Signed             Unsigned",
  221.         "                 Signed                                 Unsigned"
  222.         };
  223.  
  224. main ()
  225. {
  226.         INTEGER int a;
  227.         unsigned INTEGER int b, mask;
  228.         int ab, i, j, bit_counter, line_counter;
  229.  
  230.         a = b = SIGN_BIT;
  231.         printf ( "%s\n\n", title [ ( WORD_SIZE == 16 ) ? 0 : 1 ] );
  232.  
  233.  
  234.  
  235.  
  236.  
  237.         for ( line_counter = 0; line_counter < WORD_SIZE; line_counter++ )
  238.         {
  239.                 for ( ab = 0; ab < 2; ab++ )
  240.                 {
  241.                         mask = SIGN_BIT;
  242.                         for ( i = 0; i < NIBBLES_IN_WORD; i++ )
  243.                         {
  244.                                 for ( j = 0; j < NIBBLE_SIZE; j++ )
  245.                                 {
  246.                                         printf ( "%c", ((( ab ) ? b : a ) &
  247. mask ) ? '1' : '0' );
  248.                                         mask >>= 1;
  249.                                         }
  250.                                 printf ( " " );
  251.                                 }
  252.                         printf ( "%s", ( ab ) ? "\n" : " " );
  253.  
  254.  
  255.                         if ( ab )
  256.                         {
  257.                           b >>= 1;
  258.                                 }
  259.                         else
  260.                         {
  261.                                 a >>= 1;
  262. #if defined(FIX_COMPILER_BUG)
  263. # if (INTEGER == long)
  264.                                 a |= SIGN_BIT;    /* This is a work-around for
  265. the 3b2 compiler bug. */
  266. # endif
  267. #endif
  268.                                 }
  269.                         }
  270.                 }
  271.         }
  272.  
  273.         /  * ----------------------------------------- */
  274.  
  275.  
  276.   This little program might well produce some interesting surprises on
  277. your machine in the same way it did on mine. I have an AT&T 3b2/400 and use the K & R style compiler. Interestingly, the above program did whatI expected it to do when the integers were short, the sign bit is extended,but when the integers are long the sign bit is NOT extended. In this case the different behaviour is caused by the compiler always issuing a Logical Shift instruction, when it should issue a Arithmetic Shift instruction for signed integers and a Logical Shift instructon for unsigned ones. In the case of the short int the varable is loaded from memory into the register with a sign extend load instruction, this makes the Logical Shift instruction right work correctly for short ints, but not for longs.  I had to examine the assember codes output by the compiler in order to discover this.
  278.  
  279.   Here are the compiler invocation lines.
  280.  
  281.       cc -olong.shifts -DFIX_COMPILER_BUG -DINTEGER=long shifts.c
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.         and
  290.  
  291. cc -oshort.shifts -DINTEGER=short shifts.c
  292.  
  293.         Experiment with the "-DFIX_COMPILER_BUG" and see what your compiler does.
  294.  
  295.