Lecture 02 Computer Data Representation

Joseph Haugh

University of New Mexico

Class Participation Instructions

  • Put your full name and the date at the top of your card.
  • Make sure your writing is legible.

Additional Skills for Programming Success

  • Analytical and mathematical skills.
  • Algorithm development.
  • Debugging.
  • Focus, determination, and teamwork.
  • Reading and understanding code and documentation.
  • Asking for help.

Good Practices in Programming

  • Follow naming conventions.
  • Outline objectives clearly.
  • Ensure modularity and readability.
  • Apply good practices habitually.

To Attain the Learning Objectives

  • Complete the readings before class.
  • Practice the exercises
  • Writing is the key to understanding, you must write code to succeed.
  • If you are still confused then write down a muddy point during class or attend office hours.

Learning Objectives

  • Define bits and bytes.
  • Explain why and how computers use bits to represent information.
  • Describe integral types.
  • Perform the following C operations:
    • Bit-level operations
    • Logic operations
    • Shift and mask operations

Everything is Bits

  • Bits: 0 or 1.
  • Computers encode instructions, numbers, sets, strings, etc., using bits.
  • Why bits? Electronic implementation:
    • Bistable elements for storage.
    • Reliable transmission over noisy channels.

Binary and Number Representation

  • 15, 21310 == 111011011011012
  • 1.2010 == $1.\overline{0011}_2$
  • 1.521310 * 104 == 1.11011011011012 * 213

Encoding Byte Values

  • A byte = 8 bits.
    • Binary: 000000002 to 111111112.
    • Decimal: 010 to 25510.
    • Hexadecimal: 0016 to FF16.
    • Write FA1D37B16 in C as
      • 0xFA1D37B or
      • 0xfa1d37b

Bits, Bytes, and Integers

  • Representing information as bits.
  • Bit-level manipulations:
    • Unsigned and signed integers.
    • Conversion, casting, expanding, truncating.
    • Arithmetic operations (addition, negation, shifting).
  • Representations in memory, pointers, strings

Practice Conversion

  • Convert 0x39A7F8 to binary:
  • Convert 11001001011110112 to hexadecimal:

Practice Conversion

  • Convert 0x39A7F8 to binary:

    3 9 A 7 F 8
    0011 1001 1010 0111 1111 1000
  • Convert 1100100101111011 to hexadecimal:

Practice Conversion

  • Convert 0x39A7F8 to binary:

    3 9 A 7 F 8
    0011 1001 1010 0111 1111 1000
  • Convert 1100100101111011 to hexadecimal:

    1100 1001 0111 1011
    C 9 7 B

Boolean Algebra Basics

  • True = 1, False = 0.
  • Operators:
    • AND (A & B = 1 if both A = 1 and B = 1).
    • OR (A | B = 1 if either A = 1 or B = 1).
    • NOT (~A = 1 if A = 0).
    • XOR (A ^ B = 1 if either A = 1 or B = 1, but not both).

Boolean Algebra Operations

  • Operators apply bitwise:

    01101001 & 01010101 = 01000001
    01101001 | 01010101 = 01111101
    01101001 ^ 01010101 = 00111100
    ~01010101 = 10101010
  • All of the properties of boolean algebra apply (p. 52)

Boolean Algebra

  • Boolean operations |, &, and ~ operating on bit vectors of length w form a Boolean Algebra for integer w > 0
  • Boolean Algebra has many properties of arithmetic over integers.
    • E.g. & distributes over |
    • a & (b | c) = (a & b) | (a & c)
    • Just as multiplication distributes over addition
  • Boolean | distributes over & but in integer arithmetic, addition does not distribute over multiplication, i.e. a + (b * c) is not = to (a + b) * (a + c)

Boolean Rings

  • Operations (^, &, ~) on bit vectors form Boolean rings.
  • Many properties of Boolean rings also hold on integer arithmetic
  • In integer arithmetic, every integer x has an additive inverse; the equivalent to addition in Boolean is ^
  • Some interesting results arise from this.

Why Boolean Forms Matter

Why Boolean Forms Matter

  • Understanding logic and set representations.
  • Facilitating bitwise manipulation in programming.

Learning Objectives

  • Define bits and bytes.
  • Explain why and how computers use bits to represent information.
  • Describe integral types.
  • Perform the following C operations:
    • Bit-level operations
    • Logic operations
    • Shift and mask operations

Integral Types

  • integral means “of or denoted by an integer”
  • Simplest machine data types are ones that represent integral types
  • You should already be familiar with the basics of this, including in C
  • Characters are just 8-bit integers
  • Used for booleans (0 = false, non-zero = true, implementation)
  • Basic boolean logic – AND, OR, XOR, NOT, shifts and masks
  • These can be used for interesting things in C
  • In general, in assignments (Datalab, for example) you may not use high-level logical operations.

Learning Objectives

  • Define bits and bytes.
  • Explain why and how computers use bits to represent information.
  • Describe integral types.
  • Perform the following C operations:
    • Bit-level operations
    • Logic operations
    • Shift and mask operations

Example: Representing & Manipulating (finite) Sets

  • Representation
    • bit vector of length w, used to represent subsets of {0, …, w–1}

    • aj = 1 if j ∈ A

    • { 0, 3, 5, 6 }

      0 1 1 0 1 0 0 1
      7 6 5 4 3 2 1 0
    • { 0, 2, 4, 6 }

      0 1 0 1 0 1 0 1
      7 6 5 4 3 2 1 0

Example: Representing & Manipulating (finite) Sets

  • 01101001 { 0, 3, 5, 6 }

  • 01010101 { 0, 2, 4, 6 }

  • set operations performed as logical operations over bits

    op name result
    & Intersection 01000001 { 0, 6 }
    | Union 01111101 { 0, 2, 3, 4, 5, 6 }
    ^ exclusive or 00111100 { 2, 3, 4, 5 }
    ~ Complement(of 2nd) 10101010 { 1, 3, 5, 7 }

Bit-Level Operations in C

  • Operations &, |, ~, ^ available in C
    • Apply to any “integral” data type
    • Views arguments as bit vectors
  • Examples (Char data type = 8 bits) Steps:
    • ~ 0x41 == 0xBE
      • ~ 010000012 == 101111102
    • ~ 0x00 == 0xFF
      • ~000000002 -> 111111112
    • 0x69 & 0x55 == 0x41
      • 011010012 & 010101012 == 010000012
    • 0x69 | 0x55 == 0x7D
      • 011010012 | 010101012 == 011111012

Contrast: Logic Operations in C (Sec. 2.1.8)

  • Contrast to Logical Operators
    • &&, ||, !
      • View 0 as “False”
      • Anything nonzero as “True”
      • Always return 0 or 1
      • Early termination (see last e.g. below)
  • Examples (char data type)
    • !0x41 == 0x00 (false) NOT(True) == False
    • !0x00 == 0x01 (true)
    • !!0x41 == 0x01 (true)
    • 0x69 && 0x55 == 0x01
    • 0x69 || 0x55 == 0x01
    • p && *p (avoids null pointer access)

Shift Operations (Sec. 2.1.9)

  • Left shift (x << y):
    • Shifts bit-vector x left by y positions.
    • Fills with 0s on the right.
  • Right shift (x >> y):
    • Logical shift: fills with 0s on the left.
    • Arithmetic shift: replicates the most significant bit on the left.
Argument x 01100010
<< 3 00010000
Log. >> 2 00011000
Arith. >> 2 00011000
Argument x 10100010
<< 3 00010000
Log. >> 2 00101000
Arith. >> 2 11101000

Practice: Shift and Mask

  • For x in hex
    • convert to binary first, then perform the shift
    • x << 3 (provide result in binary and hex)
    • x >> 2 (logical)
    • x >> 2 (arithmetic)
  • x = 0xC3, 0x75, 0x87, 0x66
  • Problem 2.16 (p.58):

Known Muddy Points

  • Ox is the prefix that is attached to a number in C, when you want to indicate the number is a hexadecimal value.
  • A vector of bits is simply a sequence of bits of some length n.
  • 1 byte = 8 bits