Integer Bases Exercises

Thanks to Andrew Benson for developing these materials!

Solutions

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 int and 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!

Solutions

p1 (integer sizes)

How many bytes do the following types take up on a common 64-bit computer such as the Myth machines?

  1. unsigned long
  2. short
  3. signed 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?

  1. unsigned long
  2. short
  3. signed 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)

  1. You are given 0b1110_1011, a signed char. Determine its decimal value.
  2. You are given 0b1110_1011, a unsigned char. Determine its decimal value.
  3. 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?

  1. Alpha: (x+y)/2
  2. Bravo: x/2 + y/2
  3. Charlie: x + (y-x)/2

Integer Casting Exercises

Solutions

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.

  1. long x = (long)2;
  2. long x = 2;
  3. int x = 7U;
  4. 3L > 5UL
  5. 1 / 0U

p2 (casts in comparisons)

Evaluate each of the following boolean expressions (to true or false). Make sure you understand why!

  1. -8 < 5
  2. -8 < 5U
  3. -8U < 5U
  4. -8 < (signed int) 5U
  5. -8 < (unsigned int) 5

p3 (truncating casts)

Evaluate each of the following boolean expressions (to true or false). Make sure you understand why!

  1. (unsigned char) 256 > 1
  2. (unsigned char) 257 > 1
  3. (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;