/
bitManipulation.c
623 lines (502 loc) · 20.4 KB
/
bitManipulation.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
/*********************************************************************************************************
MIT License
Copyright (c) 2012 Samira C. Oliva
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
The Program simulats a theater stage has a set of computer controlled lights. There are 16
theater lights hand working the lights can run your program.
Professor Delia-Manuela Gârbacea
CIS 26B - Advanced C Programming
Lab #4
Written by: Samira C. Oliva
31 May 2012
*********************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "sami.h" //switch to samira.h to run with gcc
void printMenu();
void getUserOption(unsigned short** lights, unsigned short** overLayPattern,int *startBit, int *numBits);
void turnOnAllLights(unsigned short** lights);
void turnOnARangeOfLights(unsigned short** lights, int startBit, int numBits);
void turnOffAllLights(unsigned short** lights);
void turnOffARangeOfLights(unsigned short** lights, int startBit, int numBits);
void overLayOnOffPatternOntoLightConfig(unsigned short** lights,
unsigned short **overLayPattern, int starBit, int numBits);
void printBits(unsigned short wrd);
int testBit(unsigned short wrd, int bit_to_test);
void getOverlayPatternAndStartBit(unsigned short **overLayPattern, int *startBit, int *numBits);
void freeMemory(unsigned short* lights, unsigned short* overLayPattern,int *startBit, int *numBits);
#define FLUSH while( getchar() != '\n')
int main(void)
{
unsigned short *stageLights, *overLayPattern;
int *startBit, *numBits;
stageLights = (unsigned short*)Malloc(sizeof(unsigned short));
overLayPattern = (unsigned short*)Malloc(sizeof(unsigned short));
startBit = (int *)Malloc(sizeof(int));
numBits = (int *)Malloc(sizeof(int));
printMenu();
getUserOption(&stageLights, &overLayPattern, startBit, numBits);
freeMemory(stageLights, overLayPattern, startBit, numBits);
return 0;
}
//------------------------------------------------------------------------
void printMenu()
{
printf("\n\nPlease select an option from the following menu\n\n");
printf("----------------------------------------------------------------------\n");
printf(": 1) turn on all lights :\n");
printf(": 2) turn on center stage lights (lights 5-10) :\n");
printf(": 3) turn on left stage lights (lights 11-15) :\n");
printf(": 4) turn on right stage lights (lights 0-4) :\n");
printf(": 5) turn off all lights :\n");
printf(": 6) turn off center stage lights :\n");
printf(": 7) turn off left stage lights :\n");
printf(": 8) turn off right stage lights :\n");
printf(": 9) overlay on/off pattern onto light configuration :\n");
printf(": 10) stop printing the menu :\n");
printf(": 11) print the menu again :\n");
printf(": 12) quit the menu :\n");
printf("----------------------------------------------------------------------\n\n");
}
//------------------------------------------------------------------------
// Functions gets prompts the user for their option listed on the menu
// from printMenu() printed prior to this function
//
// pre: stage lights UH
//
// post: performs a number of operations on the stage lights by calling
// the appropriate function while user is willing to do so
//
//void getUserOption(unsigned short* lights)
void getUserOption(unsigned short** lights, unsigned short** overLayPattern, int *startBit, int *numBits)
{
int userOption, endProgram, emptyReply;
char buffer[128], *line, *endPtr, printFlag;
printFlag = endProgram = 1;
emptyReply = 0;
while(gets(buffer), (endProgram))
{
if(*buffer == '\0') //if empty response
{ //re-prompt user
while (*buffer == '\0')
{
printMenu();
gets(buffer);
}
}
strcat(buffer, "\n");
line = strtok(buffer, "\040\t\n");
userOption = (int)strtol(line, &endPtr, 10);
switch (userOption)
{
case 1:
turnOnAllLights(lights);
break;
case 2:
turnOnARangeOfLights(lights, 5, 6);
break;
case 3:
turnOnARangeOfLights(lights, 11, 6);
break;
case 4:
turnOnARangeOfLights(lights, 0, 5);
break;
case 5:
turnOffAllLights(lights);
break;
case 6:
turnOffARangeOfLights(lights, 5, 6);
break;
case 7:
turnOffARangeOfLights(lights, 11, 6);
break;
case 8:
turnOffARangeOfLights(lights, 0, 5);
break;
case 9:
getOverlayPatternAndStartBit(overLayPattern, startBit, numBits);
overLayOnOffPatternOntoLightConfig(lights, overLayPattern, *startBit, *numBits);
break;
case 10:
printFlag = 0;
break;
case 11:
printMenu();
break;
case 12:
endProgram = 0;
break;
default:
printf("Please enter a numeric value (1 - 12) from\n"
"the menu option\n");
break;
} //switch
if(endProgram)
printBits(**lights);
if(printFlag && endProgram && emptyReply)
printMenu();
}
}
//------------------------------------------------------------------------
// Function tuns on all the stage lights held in an unsigned short of
// 16 bits
//
// pre: unsigned short* lights
//
// post: all of the stage lights are turned ON
//
void turnOnAllLights(unsigned short** lights)
{
unsigned short ones = 0;
ones = ~ones;
*(*lights) |= ones;
}
//------------------------------------------------------------------------
// Function turns ON a range of lights.
// Center Stage : 5 - 10
// Left Stage : 11 - 15
// Right Stage : 0 - 4
//
// pre: unsigned short* lights
//
// post: a given range of lights is turned ON
//
void turnOnARangeOfLights(unsigned short** lights, int startBit, int numBits)
{
unsigned short mask;
turnOffAllLights(lights);
mask = (~((unsigned short) ~0 << numBits) << startBit);
*(*lights) |= mask;
}
//------------------------------------------------------------------------
// Function turns off all the stage lights
//
// pre: unsigned short** lights
//
// post: all of the lights are turned off
//
void turnOffAllLights(unsigned short** lights)
{
unsigned short zero;
zero = 0;
*(*lights) &= zero;
}
//------------------------------------------------------------------------
// Function turns OFF a range of lights.
// Center Stage : 5 - 10
// Left Stage : 11 - 15
// Right Stage : 0 - 4
//
// pre: unsigned short* lights
//
// post: a given range of lights is turned OFF
//
void turnOffARangeOfLights(unsigned short** lights, int startBit, int numBits)
{
unsigned short mask;
turnOnAllLights(lights);
mask = ~(~((unsigned short) ~0 << numBits) << startBit);
*(*lights) &= mask;
}
//------------------------------------------------------------------------
// Function prompts the user for the over lay pattern in binary format
// 1's and 0s as well as the start bit.
//
// pre: nothing
//
// post: unsigned short *overLayPattern, int *startBit, int *numBits
// are assigned values
//
void getOverlayPatternAndStartBit(unsigned short **overLayPattern, int *startBit, int *numBits)
{
char buffer[128], *endPtr, *start, *pattern;
int valid, limit, validPattern;
valid = validPattern = 0;
while((!valid) || (!validPattern))
{
printf("\nPlease enter the over lay pattern and start bit\n"
"example: 1011 7.\n->\n");
gets(buffer);
if(*buffer == '\0') //if empty response
{ //bail-out, return to main menu
return;
}
strcat(buffer, "\n");
pattern = strtok(buffer, "\040\t");
start = strtok(NULL, "\n");
*numBits = strspn(pattern, "10"); //length of pattern binary number
if((*numBits == strlen(pattern)) && ((strlen(pattern) <= 16)))
{
validPattern = 1;
}
else
{
validPattern = 0;
printf("excess of lights (16 max) &| not 1's and 0's\n");
}
**overLayPattern = (unsigned short)strtol(pattern, &endPtr, 2);
if(start == NULL)
{
printf("NULL start bit entry\n");
valid = 0;
}
else
{
*startBit = (int)strtol(start, &endPtr, 10);
}
limit = 16 - (*startBit); //max length of over lay pattern
if((*startBit >= 0) && (*startBit <= 15) && (*numBits <= limit))//0 - 15 avoid overflow
{
valid = 1;
}//if_startBit
else
{
valid = 0;
printf("lack of space for pattern length &| starbit not valid\n");
}
}//_while
}
//------------------------------------------------------------------------
// Function performs the over lay pattern configuration on the stage
// lights
//
// pre: lights, overLayPattern, starBit, numBits
//
// post: over lay is configured onto the stage lights
//
void overLayOnOffPatternOntoLightConfig(unsigned short** lights,
unsigned short **overLayPattern, int starBit, int numBits)
{
*(*lights) &= ~( ~((unsigned short) ~0 << numBits) << starBit);
*(*lights) |= (*(*overLayPattern)) << starBit;
}
//------------------------------------------------------------------------
// Function with test a bit wether it is a 1 or 0
//
// pre: the wrd unsigned short and the bit_to_test in the wrd
//
// post: returns a 1 if bit_to_test is set and 0 if it is unset
//
int testBit(unsigned short wrd, int bit_to_test)
{
wrd >>= bit_to_test;
wrd &= 1;
return wrd;
}
//------------------------------------------------------------------------
// Function prints the bits an unsigned short wrd
//
// pre: unsigned short wrd
//
// post: bits are printed
//
void printBits(unsigned short wrd)
{
int testbit(unsigned short wrd, int bit_to_test);
int idx, bitCount, afterNbits;
bitCount = 0;
afterNbits = 4;
for (idx = 15; idx >= 0; idx--)
{
printf("%1d", testBit(wrd, idx));
bitCount++;
if(bitCount == afterNbits)
{
printf("\040");
bitCount = 0;
}
}
printf("\n\n");
}
//------------------------------------------------------------------------
// Deallocates any dynamically allocated memory
//
// pre: stage lights, overLayPattern, startBit, and numBits
//
// post: stage lights, overLayPattern, startBit, and numBits are freed
//
void freeMemory(unsigned short* lights, unsigned short* overLayPattern,int *startBit, int *numBits)
{
free(lights);
free(overLayPattern);
free(startBit);
free(numBits);
}
/***
-------- Test Runs/Output ---->
// run # 1 =====================================================================
Please select an option from the following menu
----------------------------------------------------------------------
: 1) turn on all lights :
: 2) turn on center stage lights (lights 5-10) :
: 3) turn on left stage lights (lights 11-15) :
: 4) turn on right stage lights (lights 0-4) :
: 5) turn off all lights :
: 6) turn off center stage lights :
: 7) turn off left stage lights :
: 8) turn off right stage lights :
: 9) overlay on/off pattern onto light configuration :
: 10) stop printing the menu :
: 11) print the menu again :
: 12) quit the menu :
----------------------------------------------------------------------
warning: this program uses gets(), which is unsafe.
10
0000 0000 0000 0000
1
1111 1111 1111 1111
2
0000 0111 1110 0000
3
1111 1000 0000 0000
4
0000 0000 0001 1111
5
0000 0000 0000 0000
6
1111 1000 0001 1111
7
0000 0111 1111 1111
8
1111 1111 1110 0000
9
Please enter the over lay pattern and start bit
example: 1011 7.
->
1111 0
1111 1111 1110 1111
9
Please enter the over lay pattern and start bit
example: 1011 7.
->
1111 78
lack of space for pattern length &| starbit not valid
Please enter the over lay pattern and start bit
example: 1011 7.
->
1111
excess of lights (16 max) &| not 1's and 0's
NULL start bit entry
lack of space for pattern length &| starbit not valid
Please enter the over lay pattern and start bit
example: 1011 7.
->
11110000111100001111 3
excess of lights (16 max) &| not 1's and 0's
lack of space for pattern length &| starbit not valid
Please enter the over lay pattern and start bit
example: 1011 7.
->
1111111111111111
excess of lights (16 max) &| not 1's and 0's
NULL start bit entry
lack of space for pattern length &| starbit not valid
Please enter the over lay pattern and start bit
example: 1011 7.
->
/// empty reply .: return to main menu
1111 1111 1111 1111 /// enter option 5 (menu was turned off from
/// printing previously
5
0000 0000 0000 0000
// run # 2 ===========================================================
Please select an option from the following menu
----------------------------------------------------------------------
: 1) turn on all lights :
: 2) turn on center stage lights (lights 5-10) :
: 3) turn on left stage lights (lights 11-15) :
: 4) turn on right stage lights (lights 0-4) :
: 5) turn off all lights :
: 6) turn off center stage lights :
: 7) turn off left stage lights :
: 8) turn off right stage lights :
: 9) overlay on/off pattern onto light configuration :
: 10) stop printing the menu :
: 11) print the menu again :
: 12) quit the menu :
----------------------------------------------------------------------
warning: this program uses gets(), which is unsafe.
1
1111 1111 1111 1111
//empty entry
//user is repromted
Please select an option from the following menu
----------------------------------------------------------------------
: 1) turn on all lights :
: 2) turn on center stage lights (lights 5-10) :
: 3) turn on left stage lights (lights 11-15) :
: 4) turn on right stage lights (lights 0-4) :
: 5) turn off all lights :
: 6) turn off center stage lights :
: 7) turn off left stage lights :
: 8) turn off right stage lights :
: 9) overlay on/off pattern onto light configuration :
: 10) stop printing the menu :
: 11) print the menu again :
: 12) quit the menu :
----------------------------------------------------------------------
Please select an option from the following menu
----------------------------------------------------------------------
: 1) turn on all lights :
: 2) turn on center stage lights (lights 5-10) :
: 3) turn on left stage lights (lights 11-15) :
: 4) turn on right stage lights (lights 0-4) :
: 5) turn off all lights :
: 6) turn off center stage lights :
: 7) turn off left stage lights :
: 8) turn off right stage lights :
: 9) overlay on/off pattern onto light configuration :
: 10) stop printing the menu :
: 11) print the menu again :
: 12) quit the menu :
----------------------------------------------------------------------
// run # 3 =====================================================================
Please select an option from the following menu
----------------------------------------------------------------------
: 1) turn on all lights :
: 2) turn on center stage lights (lights 5-10) :
: 3) turn on left stage lights (lights 11-15) :
: 4) turn on right stage lights (lights 0-4) :
: 5) turn off all lights :
: 6) turn off center stage lights :
: 7) turn off left stage lights :
: 8) turn off right stage lights :
: 9) overlay on/off pattern onto light configuration :
: 10) stop printing the menu :
: 11) print the menu again :
: 12) quit the menu :
----------------------------------------------------------------------
warning: this program uses gets(), which is unsafe.
1
1111 1111 1111 1111
9
Please enter the over lay pattern and start bit
example: 1011 7.
->
//user bails out
1111 1111 1111 1111 //previous light setting printed
-------- Memory Leaks -------->
valgrind --tool=memcheck --leak-check=full --show-reachable=yes ./cN4exe
==578== LEAK SUMMARY:
==578== definitely lost: 0 bytes in 0 blocks
==578== indirectly lost: 0 bytes in 0 blocks
==578== possibly lost: 0 bytes in 0 blocks
==578== still reachable: 10,158 bytes in 34 blocks
==578== suppressed: 0 bytes in 0 blocks
***/