-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathch20_exercises.c
More file actions
495 lines (388 loc) · 9.17 KB
/
Copy pathch20_exercises.c
File metadata and controls
495 lines (388 loc) · 9.17 KB
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
/*
* ch20_exercises.c
*
* Created on: May 18, 2020
* Author: Mahmoud Hamdy
*/
// Q1
/*
* a) i = 8, j = 9
* i >> 1 + j >> 1 = 8 >> 1 + 9 >> 1 = 8 >> 10 >> 1
* = ((8 >> 10) >> 1) = 0 >> 1 = 0
*
* Output: 0
*
*
* b) i = 1
* i & ~i = 1 & (~1) = 0000 0000 0000 0001 & 1111 1111 1111 1110
* = 0000 0000 0000 0000 = 0
*
* Output: 0
*
*
* c) i = 2, j = 1, k = 0
* ~i & j ^ k = (((~i) & j) ^ k) = (((~2) & 1) ^ 0)
* = (((~0000 0000 0000 0010) & 0000 0000 0000 0001) ^ 0000 0000 0000 0000)
* = ((1111 1111 1111 1101 & 0000 0000 0000 0001) ^ 0000 0000 0000 0000)
* = (0000 0000 0000 0001 ^ 0000 0000 0000 0000) = 0000 0000 0000 0001 = 1
*
* Output: 1
*
*
* d) i = 7, j = 8, k = 9
* i ^ j & k = (i ^ (j & k)) = (7 ^ (8 & 9)) =
* = (0000...0111 ^ (0000...1000 & 0000...1001))
* = (0000...0111 ^ 0000...1000) = 0000...1111 = 15
*
* Output: 15
*
*/
//----------------------------------
// Q2
/*
* Toggling a bit is done by using exclusive or bitwise operator (^)
* with a mask consisting of 1 shifted by the desired position:
*
* Examples to toggle bit 4 in i:
*
* i = i ^ 0x10; // or i ^= 0x10;
* i ^= 1 << 4;
*
*/
//----------------------------------
// Q3
/*
* x ^= y ---> x = x ^ y --->(1)
*
* y ^= x ---> y = y ^ x ---> from (1): y = y ^ (x ^ y) = y ^ y ^ x
* = 0 ^ x = x ----> y = x --->(2)
*
* x ^= y ---> x = x ^ y ---> from (1) & (2): x = (x ^ y) ^ (x)
* = x ^ x ^ y = 0 ^ y = y --->(3)
*
* From (2) & (3): values of x and y are swapped
*
* The result: swapping x and y
*
*/
//----------------------------------
// Q4 + test script
/*
#include <stdio.h>
#define MK_COLOR(r, g, b) ((long) ((b) << 16) | ((g) << 8) | (r))
int main(void)
{
printf("color: %ld\n", MK_COLOR(97, 65, 66)); // R = 0x61, G = 0x41, B = 0x42
printf("color in hex: %#.8x", (unsigned int)MK_COLOR(97, 65, 66));
return 0;
}
*/
//----------------------------------
// Q5 + test script
/*
#include <stdio.h>
#define GET_RED(color) ((u8) ((color) & 0xFF))
#define GET_GREEN(color) ((u8) (((color) >> 8) & 0xFF))
#define GET_BLUE(color) ((u8) (((color) >> 16) & 0xFF))
typedef unsigned char u8;
int main(void)
{
long color = 4342113; // R = 0x61, G = 0x41, B = 0x42
u8 red = GET_RED(color), green = GET_GREEN(color), blue = GET_BLUE(color);
printf("Color = %ld, Color in hex = %#lx\n", color, color);
printf("Red = %d, Red in hex = %#x\n", red, red);
printf("Green = %d, Green in hex = %#x\n", green, green);
printf("Blue = %d, Blue in hex = %#x\n", blue, blue);
return 0;
}
*/
//----------------------------------
// Q6 + test script
/*
#include <stdio.h>
unsigned short swap_bytes(unsigned short i); // For (a) and (b)
int main(void)
{
unsigned short hex_dec;
SEB();
printf("Enter a hexadecimal number (up to four digits): ");
scanf("%hx", &hex_dec);
printf("Number with bytes swapped: %hx\n", swap_bytes(hex_dec));
return 0;
}
*/
// a) Non-condensed form
/*
unsigned short swap_bytes(unsigned short i)
{
unsigned short low_byte = i >> 8, high_byte = i << 8;
return high_byte | low_byte;
}
*/
// b) condensed form
/*
unsigned short swap_bytes(unsigned short i)
{
return i << 8 | i >> 8;
}
*/
//----------------------------------
// Q7 + test script
/*
unsigned int rotate_left(unsigned int i, int n);
unsigned int rotate_right(unsigned int i, int n);
#include <stdio.h>
int main(void)
{
unsigned int val = 0x12345678;
int rot_val = 4;
printf("Original value = %#x\n", val);
printf("Left-rotated value by %d = %#x\n", rot_val, rotate_left(val, 4));
printf("Right-rotated value by %d = %#x\n", rot_val, rotate_right(val, 4));
return 0;
}
unsigned int rotate_left(unsigned int i, int n)
{
return (i << n) | (i >> (sizeof(i) * 8 - n));
}
unsigned int rotate_right(unsigned int i, int n)
{
return (i >> n) | (i << (sizeof(i) * 8 - n));
}
*/
// Alternative implementation (assuming that int size is 4 bytes)
/*
unsigned int rotate_left(unsigned int i, int n)
{
return (i << n) | (i >> (32 - n));
}
unsigned int rotate_right(unsigned int i, int n)
{
return (i >> n) | (i << (32 - n));
}
*/
//----------------------------------
// Q8
/*
* a) ~(~0 << n) = ~(0xFFFFFFFF << n) ---> first n bits are ones and
* otherwise is zero
*
* b) Returns a bit-field of length n-bits and with position m as the
* most significant bit
*
* Explanation:
* Let i = 0x12345678, m = 3, n = 4
*
* i >> (m + 1 - n) = i >> (3 + 1 - 4) = i >> 0 = i
* ~(~0 << n) = ~(0xFFFFFFFF << 4) = ~(0xFFFFFFF0) = 0x0000000F = 0xF
* i & 0xF = 0x12345678 & 0x0000000F = 0x00000008 = 0x8 = 1000
* Most significant bit of 1000 is bit 3 (and m = 3)
*
*/
//----------------------------------
// Q9 + test script
/*
#include <stdio.h>
int count_ones(unsigned char ch); // For (a) and (b)
int main(void)
{
unsigned char n = 9;
printf("Number of 1s in %d: %d\n", n, count_ones(n));
return 0;
}
*/
// a) With a loop
/*
int count_ones(unsigned char ch)
{
int count = 0;
do
{
count += (ch & 1) ? 1 : 0;
} while(ch >>= 1);
return count;
}
*/
// b) Without a loop
/*
int count_ones(unsigned char ch)
{
return (ch & 1) + ((ch) ? count_ones(ch >> 1) : 0);
}
*/
//----------------------------------
// Q10 + test script
/*
#include <stdio.h>
unsigned int reverse_bits(unsigned int n);
int main(void)
{
unsigned int n = 0xA0A0A0A0;
printf("Original number = %#.8X\n", n);
printf("Reversed number = %#.8X\n", reverse_bits(n));
return 0;
}
unsigned int reverse_bits(unsigned int n)
{
// Start from the position of most and least significant bits
unsigned int high_bit = sizeof(n) * 8 - 1, low_bit = 0;
for (; low_bit < high_bit; low_bit++, high_bit--)
if ((n >> high_bit & 1) != (n >> low_bit & 1))
n ^= ((1 << high_bit) | (1 << low_bit));
return n;
}
*/
//----------------------------------
// Q11
/*
* Since the bitwise operators '&', '^', and '|' have lower precedence
* than relational and equality operators. The condition will always fail
* no matter how many control keys are pressed as the first condition to
* be evaluated is: (SHIFT_BIT | CTRL_BIT | ALT_BIT) == 0 which is always
* false if either BIT is set.
*
* If any bit is set the condition will be: if(key_code & false) which is
* false but this is incorrect.
*
* To fix this condition, we have to add parentheses to prioritize the
* bitwise &:
*
* if((key_code & (SHIFT_BIT | CTRL_BIT | ALT_BIT)) == 0)
*
*
*/
//----------------------------------
// Q12
/*
* Since bitwise shift operators have lower precedence than arithmetic
* operators, the addition operation is evaluated first leading to incorrect
* return value. To fix this code, we have to prioritize the shift operation
* by adding parentheses when using bitwise shift operator:
*
*/
// correction + test script
/*
#include <stdio.h>
unsigned short create_short(unsigned char high_byte, unsigned char low_byte);
int main(void)
{
unsigned char x = 0xab, y = 0xcd;
unsigned short z = create_short(x, y);
printf("z = %#x\n", z);
return 0;
}
unsigned short create_short(unsigned char high_byte, unsigned char low_byte)
{
return (high_byte << 8) + low_byte;
}
*/
//----------------------------------
// Q13
/*
* n &= n - 1, Let n is 8-bits and n = 7 = 0x07 = 0b00000111
*
* Once:
* n &= n - 1 = n & (n - 1) = 0b0000_0111 & 0b0000_0110 = 0b0000_0110
*
* More than once:
* n &= n - 1 = n & (n - 1) = 0b0000_0110 & 0b0000_0101 = 0b0000_0100
*
* Effect: This statement clears the rightmost bit with value 1
*
*/
// Script to monitor the behavior
/*
#include <stdio.h>
void print_binary(unsigned int n);
void strange_function(unsigned int n);
int main(void)
{
unsigned int i, j, k;
i = 10;
j = 95;
k = 235;
strange_function(i);
strange_function(j);
strange_function(k);
return 0;
}
void strange_function(unsigned int n)
{
for (; n; n &= n - 1)
print_binary(n);
putchar('\n');
}
void print_binary(unsigned int n)
{
int i = sizeof(n) * 8;
for (; i >= 0; i--)
printf("%d", (n >> i) & 1);
putchar('\n');
}
*/
//----------------------------------
// Q14
/*
typedef struct
{
unsigned int fraction: 23, exponent: 8, sign: 1;
} IEEE_Float;
*/
// Note: This code will tell you your processor's endianness
/*
int main(void)
{
int n = 1;
if(*(char *)&n == 1)
printf("Little endian\n");
else
printf("Big endian\n");
return 0;
}
*/
//----------------------------------
// Q15
/*
* a) Because some compilers treat the field's high-order bit as a sign bit,
* but others don't. So, the compilers that treat it as a sign bit will have
* the program print -1 and vice versa.
*
* b) To avoid this ambiguity, declare all bit-fields to be either
* unsigned int or signed int.
*
*/
//----------------------------------
// Q16 + test script
/*
#include <stdio.h>
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned long DWORD;
union
{
struct
{
DWORD EAX, EBX, ECX, EDX;
} dword;
struct
{
WORD AX, EAXH, BX, EBXH, CX, ECXH, DX, EDXH;
} word;
struct
{
BYTE AL, AH, EAXH_L, EAXH_H;
BYTE BL, BH, EBXH_L, EBXH_H;
BYTE CL, CH, ECXH_L, ECXH_H;
BYTE DL, DH, EDXH_L, EDXH_H;
} byte;
} regs;
int main(void)
{
regs.byte.AH = 0xab;
regs.byte.AL = 0xcd;
printf("AX = %#X, EAX = %#.8lX\n", regs.word.AX, regs.dword.EAX);
regs.dword.EAX = 0xabcdeeff;
printf("AX = %#.4X, AH = %#X, AL = %#X\n", regs.word.AX, regs.byte.AH, regs.byte.AL);
return 0;
}
*/