forked from cirosantilli/cpp-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operator.c
722 lines (564 loc) · 17.5 KB
/
operator.c
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
/* # Operators */
#include "common.h"
int* int_ptr_func_int_ptr(int *ip) {
(*ip)++;
return ip;
}
int int_func_int(int i) {
return i;
}
int main() {
/*
# Arithmetic operators
Always be on the lookout for overflows. Rockets have fallen because of them.
*/
{
/*
# Sum
# +
*/
{
/* Basic example. */
assert((1 + 2) == 3);
/* On overflow, deterministic wrap for unsigned integer types. */
{
unsigned char i;
/*i = UCHAR_MAX + (char)1;*/
/*assert(i == 0);*/
}
#ifdef UNDEFINED_BEHAVIOUR
/*
On overflow, undefined behaviour signed types.
http://stackoverflow.com/questions/3948479/integer-overflow-and-undefined-behavior
*/
{
char i;
i = CHAR_MAX + 1;
printf("CHAR_MAX + 1 = %x\n", i);
}
#endif
/*
Detect overflow:
http://stackoverflow.com/questions/199333/best-way-to-detect-integer-overflow-in-c-c
*/
}
/*
# Multiplication
# *
*/
{
assert((2 * 3) == 6);
/* Unsigned multiplication does modulo: */
{
unsigned char uc = 255;
uc *= 2;
assert(uc == 254);
}
#ifdef UNDEFINED_BEHAVIOUR
/* Undefined behaviour because signed. */
{
char c = CHAR_MAX;
c *= 2;
printf("CHAR_MAX * 2 = %x\n", c);
}
#endif
/*
Detect overflow:
http://stackoverflow.com/questions/1815367/multiplication-of-large-numbers-how-to-catch-overflow
*/
}
/*
# Division
Division is the most complex of the basic operations.
Integer division and floating point division are different
operations, which translate to different CPU instructions!
Remember that if an operation involves a floating point and an integer,
C first casts the integer type to a floating point type, then does
the floating point operation.
Division by `0` is undefined behaviour. On Linux it raises SIGFPE.
But note that handling the SIGFPE returns to just before the division. TODO check + example.
# INT_MIN / -1
`INT_MIN / -1` is undefined in 2's complement,
and 2's complement is explicitly said to be compliant to the C
integer representation standard.
*/
{
assert((4 / 2) == 2);
/* integer division */
assert((1 / 2) == 0);
/* floating poitn division */
assert((1.0 / 2.0) == 0.5);
/*
floating poitn division. `1` is cast to `double` point,
according to the usual arithmetic conversions.
*/
assert((1 / 2.0) == 0.5);
/* Same as above. */
assert((1 / (double)2) == 0.5);
}
/* # Unary minus */
{
/*
Unary minus can overflow for the smallest negative number.
TODO find quote.
*/
{
#ifdef UNDEFINED_BEHAVIOUR
printf("-INT_MIN = %x\n", -INT_MIN);
/* Just to compare. */
printf("INT_MIN = %x\n", INT_MIN);
#endif
}
/*
Unary minus on unsigned is well defined and modulo wraps.
http://stackoverflow.com/questions/8026694/c-unary-minus-operator-behavior-with-unsigned-operands
6.2.5/9 says: A computation involving unsigned operands can never overflow,
because a result that cannot be represented by the resulting unsigned integer type
is reduced modulo the number that is one greater than the largest value
that can be represented by the resulting type.
*/
{
assert(-1u == UINT_MAX);
}
}
/*
# Remainder
# %
a%b = a - (a/b)*b
# Modulus
It is *not* the mathematical modulus, as it gives different results for negative values.
It is the mathematical remainder.
http://stackoverflow.com/questions/11720656/modulo-operation-with-negative-numbers
*/
{
assert((-4 % 3) == -1);
assert((-3 % 3) == 0);
assert((-2 % 3) == -2);
assert((-1 % 3) == -1);
assert(( 0 % 3) == 0);
assert(( 1 % 3) == 1);
assert(( 2 % 3) == 2);
assert(( 3 % 3) == 0);
assert(( 3 % 3) == 0);
assert(( 4 % 3) == 1);
assert(( 5 % 3) == 2);
assert(( 6 % 3) == 0);
assert((-3 % -3) == 0);
assert((-2 % -3) == -2);
assert((-1 % -3) == -1);
assert(( 0 % -3) == 0);
assert(( 1 % -3) == 1);
assert(( 2 % -3) == 2);
assert(( 3 % -3) == 0);
assert(( 4 % -3) == 1);
#ifdef UNDEFINED_BEHAVIOUR
/*assert((1 % 0) == 0);*/
#endif
}
/* # Comparison operators */
{
assert((1 == 1) == 1);
assert((0 == 1) == 0);
assert((0 > 1) == 0);
assert((0 > 0) == 0);
assert((0 > -1) == 1);
assert((0 < 1) == 1);
assert((0 < 0) == 0);
assert((0 < -1) == 0);
assert((0 >= 1) == 0);
assert((0 >= 0) == 1);
assert((0 >= -1) == 1);
assert((0 <= 1) == 1);
assert((0 <= 0) == 1);
assert((0 <= -1) == 0);
}
}
/*
# Boolean operators
The boolean operators treat all integers as:
- 0: false
- != 0: true
The output of the boolean operators is always either 0 or 1.
*/
{
/*
# !
# Negation
*/
{
assert((!0) == 1);
assert((!1) == 0);
assert((!2) == 0);
assert((!-1) == 0);
/*
`x == 0` is equivalent to `!x`.
But its likely more readable to use `== 0` when doing comparisons,
and to leave `!x` just for boolean operations.
*/
}
/*
# ||
# or
*/
assert((0 || 0) == 0);
assert((0 || 1) == 1);
assert((1 || 0) == 1);
assert((1 || 1) == 1);
/*
# &&
# and
*/
assert((0 && 0) == 0);
assert((0 && 1) == 0);
assert((1 && 0) == 0);
assert((1 && 1) == 1);
/*
# Short circuit evaluation
For operators `||`, `&&` and `?`, the second side is only evaluated if needed.
On this example:
- 1 is evaulated to true
- || does not need to go any further, so i++ is not evaluated
Those operators also define sequence points.
*/
{
int i = 0;
1 || i++;
assert(i == 0);
1 && i++;
assert(i == 1);
}
}
/*
# Bitwise operators
On signed integers: implementation defined or UB:
http://stackoverflow.com/questions/11644362/bitwise-operation-on-signed-integer
so just never do it.
*/
{
/*
# ~
# NOT bitwise
*/
assert((~(char)0x00) == (char)0xFF);
assert((~(char)0xFF) == (char)0x00);
/*
# &
AND bitwise
# |
OR bitwise
*/
{
assert(((char)0x00 & (char)0x00) == (char)0x00);
assert(((char)0xFF & (char)0x00) == (char)0x00);
assert(((char)0xFF & (char)0xFF) == (char)0xFF);
/*
`&` and `|` have lower precedence than `==`!
Notorious design choice, since they are analogous to + and * ...
GCC warns with -Wparentheses
*/
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wparentheses"
assert(!(2 & 0 == 0 ));
#pragma GCC diagnostic pop
assert(!(2 & (0 == 0)));
assert( (2 & 0) == 0 );
}
/*
# Even
# Odd
# Find if number is even or odd
http://stackoverflow.com/questions/160930/how-do-i-check-if-an-integer-is-even-or-odd
This is another "application" of `&`.
But seems to be as fast as `%`, and is definitely less readable.
*/
{
assert((3 & 1) == 1);
assert((4 & 1) == 0);
}
}
/*
# ||
# OR bitwise
*/
assert(((char)0x00 | (char)0x00) == (char)0x00);
assert(((char)0xFF | (char)0x00) == (char)0xFF);
assert(((char)0xFF | (char)0xFF) == (char)0xFF);
/*
# ^
# XOR bitwise
*/
assert(((char)0x00 ^ (char)0x00) == (char)0x00);
assert(((char)0xFF ^ (char)0x00) == (char)0xFF);
assert(((char)0xFF ^ (char)0xFF) == (char)0x00);
/*
# bitmask
The major aplication of bitwise operators it making masks to:
- set: MASK &
- reset
- toggle
- retrieve
bits from unsigned integer fields.
These exist to allow to use one bit to store one bit,
because the minimal addressable unit on computers is 8 bits.
While such operators exist in almost all languages,
they are much more common in low level languages like C
where optimization is more present.
Only work because C fixes the binary representation of unsigned integers.
*/
/*
# <<
# >>
# Shift operators
Low level bit shifting.
For the right input, the result would
depend on which integer representation is being used,
which is not fixed by the C standard.
*/
{
assert((1u << 0u) == 1u);
assert((1u << 1u) == 2u);
assert((1u << 2u) == 4u);
assert((1u << 3u) == 8u);
assert((8u >> 0) == 8u);
assert((8u >> 1) == 4u);
assert((8u >> 2) == 2u);
assert((8u >> 3) == 1u);
assert((8u >> 4) == 0u);
assert((5u >> 1) == 2u);
/* Negative operands */
{
/* TODO undefined or implementation defined? */
printf("-1 << 1u = %d\n", -1 << 1u);
#ifdef UNDEFINED_BEHAVIOUR
/* http://stackoverflow.com/questions/4945703/left-shifting-with-a-negative-shift-count */
/*printf("2u << -1 = %d\n", 2u << -1);*/
#endif
}
/*
# Binary operator on floating point numbers
Fun, but not possible.
http://stackoverflow.com/questions/1723575/how-to-perform-a-bitwise-operation-on-floating-point-numbers
*/
{
/*1.2 << 1;*/
}
}
}
/*
# assign
*/
{
{
int i = 0;
i = 1;
assert(i == 1);
}
/*
= returns rvals
*/
{
int i;
assert((i = 1) == 1);
/*
This is why this works (and probably why it is made behave like this.
*/
{
int i, j, k;
i = j = k = 1;
/*i = (j = (k = 1));*/
assert(i == j && j == k && k == 1);
}
}
/*
# self assign initialization
Good old undefined behaviour through innocent statements.
<http://stackoverflow.com/questions/11186261/why-is-int-i-i-legal>
*/
{
int self_assign_init = self_assign_init;
printf("self_assign_init = %d\n", self_assign_init);
}
/*
# lvalue
Something that can be on the left side of an assign, such as a variable.
Every lvalue is a rvalue.
# rvalue
Something that can only be used on the right side of an assign,
but not on the left side.
*/
{
/*
In C, assign does not return lvalues.
In C++ it does.
*/
{
int i = 0, j = 1, k = 2;
/*(i = j) = k;*/
}
/*
Function returns are rvalues.
In C++, this has an exception: functions that return references return lvalues
*/
{
/*int_func_int(1) = 1;*/
/*struct_func().i = 1;*/
}
/*
A dereferenced pointer becomes an lvalue.
*/
{
int i = 0;
(*int_ptr_func_int_ptr(&i)) = 2;
assert(i == 2);
}
}
}
/*
# Increment
# Pre-increment vs post-increment
<http://stackoverflow.com/questions/24886/is-there-a-performance-difference-between-i-and-i-in-c>
Which is faster?
- in c, equal
- in c++, ++i potentially if i is a complex object
# Why the increment operator exits
Why it exists if equivalent to x=x+1?
Because there is an x86 instruction for that
Why?
- because it takes less program memory `inc eax`, instead of `sum eax,1`
- and is a *very* common instruction
What about +=, -=, etc. ?
Same thing: `ax = ax + bx` == `sum ax,bx`
*/
{
int i;
i = 0;
assert(i++ == 0);
assert(i == 1);
i = 0;
assert(++i == 1);
assert(i == 1);
i = 1;
assert(i-- == 1);
assert(i == 0);
i = 1;
assert(--i == 0);
assert(i == 0);
/*
Also works for floating point,
although the usage is much less common.
*/
double f = 0.5;
assert(f++ == 0.5);
assert(f == 1.5);
}
/*
Composite operators
Do an operation and an assign at the same time.
Exist for many operators.
Why do they exist? Assemby support probably,
as many assembly operations overwrite one of the operands.
*/
{
int i;
i = 0;
assert((i += 1) == 1);
assert(i == 1);
i = 1;
assert((i -= 1) == 0);
assert(i == 0);
i = 1;
assert((i *= 2) == 2);
assert(i == 2);
i = 2;
assert((i /= 2) == 1);
assert(i == 1);
i = 3;
assert((i %= 2) == 1);
assert(i == 1);
i = 0xFF;
assert((i &= (char)0x00) == (char)0x00);
assert((char)i == (char)0x00);
/* Same for others bitwise, except ~= which does not exist. */
{
unsigned char i = 0xFF;
i = ~i;
/* ? */
/*i~=;*/
assert((i & 0xFF) == 0);
}
}
/*
# Ternary operator
# Question mark
# ?
Called ternary operator since it is the only operator that
takes 3 inputs.
Likely exists because of CMOV instructions. But with branch preditction CMOV can be slower than if / else:
http://stackoverflow.com/questions/6754454/speed-difference-between-if-else-and-ternary-operator-in-c?lq=1#comment8007791_6754495
- http://stackoverflow.com/questions/758849/the-ternary-conditional-operator-in-c
- http://stackoverflow.com/questions/3565368/ternary-operator-vs-if-else
- http://stackoverflow.com/questions/6754454/speed-difference-between-if-else-and-ternary-operator-in-c?lq=1
*/
{
assert((1 < 2 ? 3 : 4) == 3);
assert((1 > 2 ? 3 : 4) == 4);
/* The ternary operator can also yield lvalues. */
{
int x = 0, y = 1, *xp = &x, *yp = &y;
*(1 ? xp : yp) = 10;
assert(x == 10);
}
/* The possible to initialize consts with the ternary operator. */
{
const int i = 0 ? 1 : 2;
char *s = 0 ? "a" : "b";
}
}
/*
# Comma operator
Obscure and almost useless C operator.
*/
{
/*
Commas here are part of the declarator sequence,
just like in functions calls/defs. They are not
comma operators!
*/
int i=0, a=1, b=2, c=3;
/*
ignores values on left
takes only last value on right
BAD: operations on left has no effect
*/
assert((i = 0, 1 ) == 1);
assert((i = 0, i = 1, 2) == 2);
/*
assign has precedence over comma
BAD: operation on right has no effect
*/
{
i = 2;
(i = 0), 1;
i = 0, 1;
assert(i == 0);
}
/* ERROR */
/* declaration int j does not return a value */
/*int j=0, 1;*/
/* operation on left comes first */
{
i=2;
assert((i=0, i) == 0);
i=2;
assert((i=0, i++, i) == 1);
}
/* must be parenthesis protected when passesd as function argument */
/* to differentiate from argument separtor comma */
{
int i = 0;
assert(int_func_int((i++, i)) == 1);
}
}
return EXIT_SUCCESS;
}