Skip to content

10.16 Conversion

1 Conversions between number bases

  • Convert the following binary numbers to hexadecimal.

  • \(1010_2\)

  • \(110110_2\)
  • \(100010100111_2\)
  • \(11110000_{2}\)
  • Repeat previous exercise, but convert to decimal, interpreting these vectors of bits as unsigned.
  • Convert the following hexadecimal numbers to decimal. Show the intermediate steps.

  • \(A5_{16}\)

  • \(3B_{16}\)
  • \(DAD_{16}\)
  • Convert the following vectors of bits to decimal, interpreting them as two’s complement.

  • \(1010_2\)

    \(=-8+2=-6_{10}\) - \(110110_2\)

    \(=-32+16+4+2=-10_{10}\) - \(10011111_2\)

    \(=-128+16+8+4+2+1=-97_{10}\) - \(01110000_2\)

    \(=64+32+16=112_{10}\) - Convert the following decimal numbers to vectors of 8 bits.

  • \(42_{10}\)

    \(=32+8+2=00101010_{2}\) - \(63_{10}\)

    \(=32+16+8+4+2+1=00111111_2\) - \(229_{10}\)

    \(=128+64+32+4+1=11100101_2\) - Same for the following numbers. First convert them as positive numbers, then negate them. (Remember: to apply the “two’s complement” operation to a vector of bits, invert all the bits then add 1 to the vector of bits).

  • \(-29_{10}\)

    \(29_{10}=00011101_{2}\), then \(11100011_2=-29_{10}\) - \(-66_{10}\)

    \(66_{10}=01000010_2\), then \(10111110_{2}=-66_{10}\) - \(-124_{10}\)

    \(124_{10}=01111011_{2}\), then \(10000100_{2}=-124_{10}\) - Verify the effect of the “two’s complement” operation, on the numbers: 0, -1, TMinw, and TMaxw , for any value of w.

image

2 Addition of unsigned and two’s complement numbers

  • Perform the following additions of unsigned 4-bit binary numbers. Indicate whether or not the sum overflows a 4-bit result.

  • \(1001_2 + 0100_2\)

    \(=1101_{2}\), no overflow - \(1101_2 + 1011_2\)

    \(=(1)1000_{2}\), overflow. The register will not have the first \((1)\) although it will be stored in a special register - Repeat the previous exercise, assuming that the binary numbers are in two’s complement form.

For this, we know from lecture if we perform addition of two positive signed number, it will possibly overflow to negative

If we perform addition of two negative signed number, it will possibly overflow to positive.

If we perform addition of one positive and one negative signed number, it will never overflow.

Thus the answer is

  • No
  • No, since calculation \(-3+(-5)=-8\)
  • Perform the following additions of unsigned 8-bit binary numbers. Indicate whether or not the sum overflows an 8-bit result.

  • \(10011001_2 + 01000100_2\)

    \(=11011101\), no overflow - \(11010010_2 + 10110110_2\)

    \(=(1)10001010\), overflow - Repeat the previous exercise, assuming that the binary numbers are in two’s complement form.

  • No

  • No, since the calculation \(-46-74=-120\)

Signed overflow: 负变正 or 正变负
Unsigned overflow: Truncation

3 Experiments in C

  • Consider the following C program:
main(){
  unsigned char x;
  scanf("%hhu",&x);
  printf("Stored as: %hhu\n", x);
}
  • Note that %hhu is the correct placeholder code to read an unsigned char with scanf().
  • How many bits are used to store the value of variable x? 8

image - Consider the following input values and program outputs. Propose an explanation of this behaviour.

300
Stored as: 44

1000
Stored as: 232
- Write a C program that reads two unsigned char variables, sum them and detects if the sum caused an overflow. Test your program. - Write a C program that reads two signed char variables, sum them and detects if the sum caused an overflow. Test your program. - Consider the following C snippet:

for (char i = 99; i >= 0; --i) {
  /* whatever */
}
  • How many times does the loop repeat?

100 times, since the program palses at \(i=-1\) - Same question for the following snippet:

for (unsigned char i = 99; i >= 0; --i) {
  /* whatever */
}
  • Explain the difference with the previous snippet.

Infinitely many times, since \(i=99,...,0,255,254,....\)