summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SAWTELL/c-lesson.5
blob: 949fb499fbd393a9859e31726be731bed8663343 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291

Lesson 4.

                                                                         The
operators of the language.

        I have mentioned that 'C' is a small language with most of the heavy
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.
At the time when 'C' was designed and first written these were unique for
a high level language.

  Lets start with a discussion about precedence.

        This really means that the compiler puts invisable parentheses into
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

        15 + 4 * 11 = 59

        The compiler has rendered the expression as:

        15 + ( 4 * 11 ) = 59

        Now the 'C' language has a much larger collection of operators than
just
Multiply Divide Add Subtract, in fact much too big to try to remember the
precedence of all of them. So my recomendation is to ALWAYS put in the
parentheses, except for simple arithmetic. However, for the sake of
completeness as much as anything else, here is the list.

        First up come what are called the primary-expression operators:

                ()    Function.
                []    Array.
                .     struct member ( variable ).
                ->    struct member ( pointer ).

         The unary operators:

                *     Indirection via a Pointer.
                &     Address of Variable.
                -     Arithmetic Negative.
                !     Logical Negation or Not.
                ~     Bit-wise One's Complement.
                ++    Increment.
                --    Decrement.
                sizeof  Which is self explanitary.

        Now the binary operators:

   Arithmetic Operators.

                *     Multiply.                                       My
                /     Divide.                                         Dear
                %     Modulo, or Remainder of Integer Division.
                +     Addition.                                       Aunt
                -     Subtraction.                                    Sally

         The Shifting Operators.

                >>    Bit-wise Shift to the Right.
                <<    Bit-wise Shift to the Left.

   Logical Relation Operators.

                <     Less Than.
                >     Greater Than.
                <=    Less Than or Equal.
                >=    Greater Than or Equal.
                ==    Equal.
                !=    Not Equal.

         Bit-wise Boolean Operators.

                &     Bit-wise And.
                ^     Bit-wise Exclusive-or.
                |     Bit-wise Or.

         The Logical Operators.

                &&    Logical And.
                ||    Logical Or.

   The Assignment Operators. ( They all have the same priority. )

                =     The normal assignment operator.

         The Self-referencing Assignment Operators.

                +=
                -=
                *=
                /=
                %=
                >>=
                <<=
                &=
    ^=
                |=

  Some explanation is in order here. The machine instructions in your
computer include a suit of what are called "immediate operand" instructions.
These instructions have one of the operands in a register and the other
is either part of the instruction word itself ( if it is numerically small
enough to fit ) or is the next word in the address space "immediately" after
the instruction code word. 'C' makes efficient use of this machine feature
by providing the above set of operations each of which translates directly
to its corresponding machine instruction. When the variable in question is a
'register' one, or the optimiser is in use, the compiler output is just
the one "immediate" machine instruction. Efficiency Personified!!!

        These two lines will make things clearer.

        a = 8;
        a += 2;     /* The result is 10 */

        The exclusive-or operation is very useful you can toggle any
combination
of bits in the variable using it.

        a = 7;
        a ^= 2;    /* Now a is 5 */
        a ^= 2;    /*  and back to 7. */

        Naturally, you can use the other operations in exactly the same way,
I'd like to suggest that you make a utterly simplistic little program
and have a look at the assembler code output of the compiler. Don't be
afraid of the assembler codes - they don't bite - and you will see
what I was on about in the paragraph above.

        Historical Note and a couple of Cautions.

        In the Oldend Days when 'C' was first written all the self-referencing
operations had the equals symbol and the operand around the other way.
Until quite recently ( unix system V release 3.0 ) the 'C' compiler had a
compatability mode and could cope with the old style syntax.

        A sample or test program is probably in order here.

/* ----------------------------------------- */

#include <stdio.h>

char *mes[] =
{
        "Your compiler",
        " understands",
        " does not understand",
        " the old-fashioned self-referencing style."
        };

main()
{
        int a;

        a = 5;
        a=-2;
        printf ( "%s %s %s\n", mes [ 0 ], mes [ ( a == -2 ) ? 2 : 1 ], mes [ 3
] );
        }

/* ----------------------------------------- */

        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!
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.

        a=(-2);
        a = -2;

Either is acceptable, and might save you a lot of spleen if sombody tries
to install your work of art program on an ancient machine.

  The other caution is the use of the shifting instructions with signed
and unsigned integers.

        If you shift a signed integer to the right when the sign bit is set
then in all probability the sign will be extended. Once again a little
demo program. Please cut it out of the news file with your editor
and play with it.

/* ----------------------------------------- */

#ident "#(@) shifts.c - Signed / Unsigned integer shifting demo."
#include <stdio.h>

#define WORD_SIZE ( sizeof ( INTEGER int ) * 8 )
#define NIBBLE_SIZE 4
#define NIBBLES_IN_WORD (( WORD_SIZE ) / NIBBLE_SIZE )
#define SIGN_BIT ( 1 << ( WORD_SIZE - 1 ))

char *title[] =
{ "       Signed             Unsigned",
        "                 Signed                                 Unsigned"
        };

main ()
{
        INTEGER int a;
        unsigned INTEGER int b, mask;
        int ab, i, j, bit_counter, line_counter;

        a = b = SIGN_BIT;
        printf ( "%s\n\n", title [ ( WORD_SIZE == 16 ) ? 0 : 1 ] );

        for ( line_counter = 0; line_counter < WORD_SIZE; line_counter++ )
        {
                for ( ab = 0; ab < 2; ab++ )
                {
                        mask = SIGN_BIT;
                        for ( i = 0; i < NIBBLES_IN_WORD; i++ )
                        {
                                for ( j = 0; j < NIBBLE_SIZE; j++ )
                                {
                                        printf ( "%c", ((( ab ) ? b : a ) &
mask ) ? '1' : '0' );
                                        mask >>= 1;
                                        }
                                printf ( " " );
                                }
                        printf ( "%s", ( ab ) ? "\n" : " " );
                        if ( ab )
                        {
                          b >>= 1;
                                }
                        else
                        {
                                a >>= 1;
#if defined(FIX_COMPILER_BUG)
# if (INTEGER == long)
                                a |= SIGN_BIT;    /* This is a work-around for
the 3b2 compiler bug. */
# endif
#endif
                                }
                        }
                }
        }

/* ----------------------------------------- */

  This little program might well produce some interesting surprises on
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 what
I 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.

  Here are the compiler invocation lines.

cc -olong.shifts -DFIX_COMPILER_BUG -DINTEGER=long shifts.c

        and

cc -oshort.shifts -DINTEGER=short shifts.c

        Experiment with the "-DFIX_COMPILER_BUG" and see what your compiler
does.

Copyright notice:-

(c) 1993 Christopher Sawtell.

I assert the right to be known as the author, and owner of the
intellectual property rights of all the files in this material,
except for the quoted examples which have their individual
copyright notices. Permission is granted for onward copying,
but not modification, of this course and its use for personal
study only, provided all the copyright notices are left in the
text and are printed in full on any subsequent paper reproduction.

--
 +----------------------------------------------------------------------+
 | NAME   Christopher Sawtell                                           |
 | SMAIL  215 Ollivier's Road, Linwood, Christchurch, 8001. New Zealand.|
 | EMAIL  chris@gerty.equinox.gen.nz                                    |
 | PHONE  +64-3-389-3200   ( gmt +13 - your discretion is requested )   |
 +----------------------------------------------------------------------+