0% found this document useful (0 votes)
76 views2 pages

Part I. Code Tracing (Score: - / 10 PTS) Program19.c (Bitwise Operators) Program20.c (Endianness)

The document contains code snippets and descriptions for bitwise operations and bit masking exercises. It provides guidance on format specifiers, bitwise operators, and bit masking definitions. The tasks are to trace the code snippets, predict the output, and write the expected console output for each program's main function.

Uploaded by

maki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views2 pages

Part I. Code Tracing (Score: - / 10 PTS) Program19.c (Bitwise Operators) Program20.c (Endianness)

The document contains code snippets and descriptions for bitwise operations and bit masking exercises. It provides guidance on format specifiers, bitwise operators, and bit masking definitions. The tasks are to trace the code snippets, predict the output, and write the expected console output for each program's main function.

Uploaded by

maki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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

You might also like