Week 07 Tutorial Questions
Objectives
- introduce the two's-complement method of storing negative integers in binary
- introduce the IEEE-754 standard for representing floating-point numbers in binary as a more complex binary representation of data
- introduce read and write system calls with standard streams
-
What decimal numbers do the following single-precision IEEE 754-encoded bit-strings represent?
-
0 00000000 00000000000000000000000 -
1 00000000 00000000000000000000000 -
0 01111111 10000000000000000000000 -
0 01111110 00000000000000000000000 -
0 01111110 11111111111111111111111 -
0 10000000 01100000000000000000000 -
0 10010100 10000000000000000000000 -
0 01101110 10100000101000001010000
Each of the above is a single 32-bit bit-string, but partitioned to show the sign, exponent and fraction parts.
-
-
Convert the following decimal numbers into IEEE 754-encoded bit-strings:
- 2.5
- 0.375
- 27.0
- 100.0
-
Consider the following code (that has no error checking):
unsigned char c; scanf("%c", &c); printf("%c", c);
Assume that all of the relevant
#include's are done.- From which stream does scanf read input?
- To which stream does printf write input?
- Rewrite this code using read and write instead of the stdio functions scanf and printf
-
Assume that the following hexadecimal values are 16-bit twos-complement. Convert each to the corresponding decimal value.
-
0x0013 -
0x0444 -
0x1234 -
0xffff -
0x8000
-
-
Give a representation for each of the following decimal values in 16-bit twos-complement bit-strings. Show the value in binary, octal and hexadecimal.
- 1
- 100
- 1000
- 10000
- 100000
- -5
- -100
-
On a machine with 16-bit
ints, the C expression(30000 + 30000)yields a negative result.Why the negative result? How can you make it produce the correct result?
Extra questions
The following questions are extra content not necessarily needed to cover this week's content.
Your tutor may still choose to cover some of these questions, time permitting.
-
Draw diagrams to show the difference between the following two data structures:
struct { int a; float b; } x1; union { int a; float b; } x2;
If
x1was located at&x1 == 0x1000andx2was located at&x2 == 0x2000, what would be the values of&x1.a,&x1.b,&x2.a, and&x2.b? -
How large (#bytes) is each of the following C
unions?-
union { int a; int b; } u1;
-
union { unsigned short a; char b; } u2;
-
union { int a; char b[12]; } u3;
-
union { int a; char b[14]; } u4;
-
union { unsigned int a; int b; struct { int x; int y; } c; } u5;
You may assume
sizeof(char)== 1,sizeof(short)== 2,sizeof(int)== 4. -
-
Consider the following C
unionunion _all { int ival; char cval; char sval[4]; float fval; unsigned int uval; };
If we define a variable
union _all var;and assign the following valuevar.uval = 0x00313233;, then what will each of the following printf(3)s produce:-
printf("%x\n", var.uval); -
printf("%d\n", var.ival); -
printf("%c\n", var.cval); -
printf("%s\n", var.sval); -
printf("%f\n", var.fval); -
printf("%e\n", var.fval);
You can assume that bytes are arranged from right-to-left in increasing address order.
-