Integer Bases Exercises
Thanks to Andrew Benson for developing these materials!
p1 (base conversions)
Complete the following table by hand. For binary, write 8 digits, padding with leading zeroes if necessary. Note that the underscore is there purely for readability.
| Decimal | Binary | Hexadecimal |
|---|---|---|
| 1 | ||
| 21 | ||
| 0b1000_0001 | ||
| 0x1e |
p2 (conversions in C (or not?))
Confused Cornelius posts the following question on EdStem:
I am trying to write a function that takes in an
intand converts it to hex. Here's what I've gotten so far:
int convert_to_hex(int num) { // I'm stuck here...I read over the slides and notes about doing decimal-hexadecimal conversions but I'm still confused. I can code the arithmetic, but I'm not sure what my final result should really be...?
Help Cornelius out by making a reply to his Ed question.
p3 (base conversions using gdb)
Complete the following table with the help of gdb. Please don't use some
random website that you found on Google. You know who you are.
For binary, write 8 digits, padding with leading zeroes if necessary. Note that the underscore is there purely for readability.
| Decimal | Binary | Hexadecimal |
|---|---|---|
| 254 | ||
| 0b0110_0101 | ||
| 0xd6 |
Integer Representations Exercises
Thanks to Andrew Benson for developing these materials!
p1 (integer sizes)
How many bytes do the following types take up on a common 64-bit computer such as the Myth machines?
unsigned longshortsigned char
What C code could you write to determine the above?
p2 (integer ranges)
What is the range of integral values representable by each of the following types?
unsigned longshortsigned char
p3 (signedness)
Suppose Andrew writes 0b1111_1111 on the board. He assures you that this is a
8-bit integer. What value does it represent? (Hint: trick question)
p4 (two's complement)
- You are given
0b1110_1011, asigned char. Determine its decimal value. - You are given
0b1110_1011, aunsigned char. Determine its decimal value. - You write the following code:
signed char x = -13;
What is the underlying bit pattern of the signed char?
p5 (overflow)
Alpha, Bravo, and Charlie are trying to write a C expression for the floored
average of two unsigned int's. (That is, the average rounded down to the
nearest unsigned int.) What do you think about their proposals?
- Alpha:
(x+y)/2 - Bravo:
x/2 + y/2 - Charlie:
x + (y-x)/2
Integer Casting Exercises
p1 (identifying casts)
Determine what casts (implicit or explicit) are occurring (there might be multiple!), and for each, what type is being casted to what type.
long x = (long)2;long x = 2;int x = 7U;3L > 5UL1 / 0U
p2 (casts in comparisons)
Evaluate each of the following boolean expressions (to true or false). Make
sure you understand why!
-8 < 5-8 < 5U-8U < 5U-8 < (signed int) 5U-8 < (unsigned int) 5
p3 (truncating casts)
Evaluate each of the following boolean expressions (to true or false). Make
sure you understand why!
(unsigned char) 256 > 1(unsigned char) 257 > 1(unsigned char) 258 > 1
p4 (casts in arithmetic)
What is the value of c? Be careful of the effect of implicit casts.
signed char a = -3;
unsigned char b = 1U;
signed char c = (a + b) / 2;