Lecture 03 Computer Data Representation Part 2

Joseph Haugh

University of New Mexico

Review

  • Chapter 2: secs. 2.1, 2.2.1, 2.2.2, 2.2.3
  • Notions of bits and bytes. (1 byte = 8 bits)
  • Integral types
  • Relevant operations in C for this class: bit-level operations (&, |, ^,~), logical operations (&&, ||, !), shift (left and right)

Index Card Questions

  • Why is !0x00 == 0x01 and not 0x11?
    • ! is a logical operator
    • applying ! to 0 (false) is 1 (true)
    • applying ~ to 0 (assuming 32 bits) is FFFFFFFF in hex
  • How can you tell if a binary vector is meant to represent a number or a subset?
  • How to differentiate between bitwise AND (&) and the operator & (as give me the address of)?
  • Does C have a NAND operator?

Overview

  • Use of shifts and masks
  • Encoding integers

Practice Masking Operations in C

  • Problem 2.12 p. 55: Write C expressions for
    • Least significant byte of x, with all other bits set to 0.
    • All but the least significant byte of x complemented, with least significant byte left unchanged.
    • Least significant byte set to all ones, and all other bytes of x left unchanged.
  • Do for x = 0x789ABC21 and w = 32 (any w >= 8)

Extracting and Assembling Integral Types - Shift and Mask

  • Shift/AND commonly used to extract subset of bits from integral types

    int x, y, z;
    // Pull bits 16-23 out of X
    y = (x >> 16) & 0xff;
  • OR and shift used to assemble multiple values into a single larger value

    // Construct z out of those bits and another value;
    z = (y & 0xff) | ((0x18 << 8) & 0x3f00);
  • Used in places where you pack multiple values into a single word (e.g. device drivers)

  • C unions, structs, and bit-fields are another way to do this (more later)

Problems to Work Through

  • Problems: (no answer in the book)
    • 2.59,
    • 2.61,
    • 2.68
  • With coding rules on pp. 128-129
  • Homework #1 will be posted tonight, includes some of these and some others.

Learning Objectives

  • After this session + practice you should be able to:
    • Describe how are integral types represented in the computer: unsigned and signed.
    • Define and perform conversion, casting, expanding and truncating operations on integers.
    • Define and perform integer arithmetic operations + Explain and analyze the errors derived from these operations.

Review: 1s Complement

  • How do we store negative numbers in binary?
  • Assuming the most significant bit is the sign bit (0 for positive, 1 for negative)
  • At first you might say just flip all the bits (bitwise NOT ~)
  • For example working with 4 bits: 7 (0111) would become -7 (1000)
  • This is called 1s complement
  • Why might this be problematic?

Review: 1s Complement Problems

  • How is zero represented?
    • You can have two representations of 0
    • 0 (000) becomes -0 (111)
  • How does addition behave?
    • What is 3 (011) + -3 (100)?
    • 111, -0
    • What is 3 (011) + -1 (110)?
    • You would get a carry in the 4th bit to get 1001
    • You would then to add that 1 back to the least significant 3 bits to get 010
  • Tricky stuff

Review: 1s Complement Table

3-bit one’s-complement:

Bits Unsigned Value One’s Complement Value
000 0 0
001 1 1
010 2 2
011 3 3
100 4 -3
101 5 -2
110 6 -1
111 7 -0

Review: 2s Complement

  • The procedure itself is relatively easy
  • The steps to obtain the binary of a negative number are:
    1. Produce the binary of the absolute value of the number, the MSB is the sign bit
    2. Invert all bits (bitwise NOT)
    3. Add 1 to the inverted number, ignore overflow
  • For example, lets convert 3 into -3:
    1. 011
    2. 100
    3. 101

Review: 2s Complement Benefits

  • One representation of 0
  • Try to convert 0 into -0:
    1. 0000
    2. 1111
    3. 0000
  • Additive inverse preserved:
    • 3 (011) + -3 (101) = 0000

Review: 2s Complement Table

3-bit two’s-complement:

Bits Unsigned Value Two’s Complement Value
000 0 0
001 1 1
010 2 2
011 3 3
100 4 -4
101 5 -3
110 6 -2
111 7 -1

Encoding Integers

  • Converting base 2 to base 10 (X = binary number, w = word size):

    • Unsigned: $UB2D(X) = \sum_{i=0}^{w-1} x_i * 2^i$
    • 2s complement: $TB2D(X) = -x_{w-1} * 2^{w-1} + \sum_{i=0}^{w-2} x_i * 2^i$
  • C short is 2 bytes long

    Decimal Hex Binary
    x 15213 3B 6D 00111011 01101101
    y -15213 C4 93 11000100 10010011

Try It

  • Show how to represent 1310 as an 8 bit value, MSB is the sign bit.
  • Then show how to represent −1310 in 8 bit 2’s complement notation.
  • Finally show that 1310 + −1310 = 0 using the 2’s complement representations.

Try It

  • Show how to represent 1310 as an 8 bit value, MSB is the sign bit.
    • 00001101 = 1 * 23 + 1 * 22 + 1 * 20 = 1310
  • Then show how to represent −1310 in 8 bit 2’s complement notation.
  • Finally show that 1310 + −1310 = 0 using the 2’s complement representations.

Try It

  • Show how to represent 1310 as an 8 bit value, MSB is the sign bit.
    • 00001101 = 1 * 23 + 1 * 22 + 1 * 20 = 1310
  • Then show how to represent −1310 in 8 bit 2’s complement notation.
    • 11110011 = −1 * 27 + 1 * 26 + 1 * 25 + 1 * 24 + 1 * 21 + 1 * 20 = −1310
  • Finally show that 1310 + −1310 = 0 using the 2’s complement representations.

Try It

  • Show how to represent 1310 as an 8 bit value, MSB is the sign bit.
    • 00001101 = 1 * 23 + 1 * 22 + 1 * 20 = 1310
  • Then show how to represent −1310 in 8 bit 2’s complement notation.
    • 11110011 = −1 * 27 + 1 * 26 + 1 * 25 + 1 * 24 + 1 * 21 + 1 * 20 = −1310
  • Finally show that 1310 + −1310 = 0 using the 2’s complement representations.
    • 00001101 + 11110011 = 00000000

-13 2’s Complement Step By Step

Converting −1310 to 2’s complement:

  1. Convert absolute value to binary: 1310 = 00001101

  2. Invert all the bits: 11110010

  3. Add 1: 11110011

  4. Verify, recall $TB2D(X) = -x_{w-1} * 2^{w-1} + \sum_{i=0}^{w-2} x_i * 2^i$:

    128 64 32 16 8 4 2 1
    1 1 1 1 0 0 1 1
    -128 64 32 16 0 0 2 1 = −1310

More Practice

  • Represent 2010 as an 8 bit value, MSB is the sign bit.
  • Represent −2010 as an 8 bit 2’s complement value, MSB is the sign bit.

Numeric Ranges

  • Unsigned range:
    • UMIN = 0
    • UMAX = 2w − 1
  • 2’s complement range:
    • TMIN = −2w − 1
    • TMAX = 2w − 1 − 1
Decimal Hex Binary
UMAX 65535 FF FF 11111111 11111111
TMAX 32767 7F FF 01111111 11111111
TMIN -32768 80 00 10000000 00000000
-1 -1 FF FF 11111111 11111111
0 0 00 00 00000000 00000000

Implications For C Programming

  • Many of these are constants in C:
  • #include<limits.h>
  • Declares constants such as:
    • ULONG_MAX
    • LONG_MAX
    • LONG_MIN