Course: DTPH C Programming
Trainee Number:
Trainee Name:
Exercise 6A & 6B Code Tracing Exercises
Part I. Code Tracing (Score: ____ / 10 pts)
Program19.c (Bitwise Operators) Program20.c (Endianness)
/* GUIDE */ /* GUIDE */
// (1) Format specifier: %04X // (1) Format specifier: %08X
// This displays the value as a 4-digit // This displays the value as an 8-digit
// hex number, padded with zeroes on the // hex number, padded with zeroes on the
// right as may be needed. Ex: 0x12 // right as may be needed. Ex: 0x1234
// is displayed as "0x0012". // is displayed as “0x00001234”.
// (2) “ret |= expression” is equal to:
#include <stdio.h> // “ret = ret | expression”
#define BYTE0 0x00FF #include <stdio.h>
#define BYTE1 0xFF00
#define BYTE0 0x000000FF
void func() { #define BYTE1 0x0000FF00
unsigned short us1 = 0x1234; #define BYTE2 0x00FF0000
unsigned short us2 = 0x8765; #define BYTE3 0xFF000000
unsigned short usRet; unsigned long func(unsigned long hex) {
usRet = us1 & us2; unsigned long ret = 0x00000000;
printf("%04X¥n", usRet);
printf("%08X¥n", hex);
usRet = us1 | us2; ret = (hex >> 24) & BYTE0;
printf("%04X¥n", usRet); printf("%08X¥n", ret);
ret |= (hex << 8) & BYTE2;
usRet = us1 ^ us2; printf("%08X¥n", ret);
printf("%04X¥n", usRet); ret |= (hex >> 8) & BYTE1;
printf("%08X¥n", ret);
usRet = ~us1; ret |= (hex << 24) & BYTE3;
printf("%04X¥n", usRet); printf("%08X¥n", ret);
usRet = ~us2; return ret;
printf("%04X¥n", usRet); }
}
int main() {
int main() { func(0xDEADBEEF);
func();
return 0; return 0;
} }
What is the output on the console when the What is the output on the console when the
main function is executed? (5 pts.) main function is executed? (5 pts.)
Course: DTPH C Programming
Trainee Number:
Trainee Name:
Exercise 6C & 6D Code Tracing Exercises
Part I. Code Tracing (Score: ____ / 10 pts)
Program21.c (Bit Masking) Program22.c (Bit Masking)
// *** GUIDE *** #include <stdio.h>
// "%d" - displays an int value (in base 10)
// "%04X" - displays a 4-digit hex value, #define CUSTOM_FLAG 0x0200
// left-padded with 0s #define DIST_MASK 0x03FF
// "ucRet |= expression;" is equal to
// "ucRet = ucRet | expression" unsigned short func(unsigned short usDist) {
unsigned short usRet = 0;
#include <stdio.h>
printf("%d¥n", usDist);
#define BIT03 (1 << 3) usDist = usDist & DIST_MASK;
#define BIT07 (1 << 7)
#define BIT11 (1 << 11) if ((usDist & CUSTOM_FLAG) == CUSTOM_FLAG) {
#define BIT15 (1 << 15) usRet = (DIST_MASK & ~CUSTOM_FLAG) >> 5;
}
unsigned char func(unsigned short us1) { else {
unsigned short usRet = 0; usRet = usDist >> 5;
}
// Example screen output for
// format specifier "%04X = %d¥n": printf("%d => %d¥n", usDist, usRet);
// 0180 = 384 return usRet;
printf("%04X = %d¥n", us1, us1); }
usRet = us1 & BIT03;
printf("%04X = %d¥n", usRet, usRet); int main() {
usRet |= us1 & BIT07; printf("%04X¥n", DIST_MASK);
printf("%04X = %d¥n", usRet, usRet); func(4798);
usRet |= us1 & BIT11; func(4448);
printf("%04X = %d¥n", usRet, usRet); return 0;
usRet |= us1 & BIT15; }
printf("%04X = %d¥n", usRet, usRet);
return usRet;
}
int main() {
func(42435);
return 0;
}
What is the output on the console when the What is the output on the console when the
main function is executed? (5 pts.) main function is executed? (5 pts.)