Lecture 6 Integer Representation in Computer Systems
41 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

In C, the size of integer data types is consistent across all platforms.

False (B)

What operator in C is used to determine the number of bytes a data type occupies?

sizeof

Subtraction can be transformed into addition by adding the ________ of y with 1.

negation

Match the following integer types in C with their minimum size:

<p>char = 1 byte short = 2 bytes long = 4 bytes long long = 8 bytes</p> Signup and view all the answers

What is the result of 127 + 1 when using 8-bit two's complement?

<p>-1 (C)</p> Signup and view all the answers

One's complement arithmetic is universally used by modern CPUs.

<p>False (B)</p> Signup and view all the answers

What is the primary reason modern CPUs use two's complement representation?

<p>single representation for zero</p> Signup and view all the answers

When assigning a value from a larger integer type to a smaller integer type, the value may be ________.

<p>truncated</p> Signup and view all the answers

Match the operation with its description:

<p>Integer Truncation = Assigning value from a larger to smaller type. Unsigned Integer Expansion = Assigning a smaller to a larger type, unused bits are zeroed. Signed Integer Expansion = Assigning as smaller to a larger type, bits are set to the same value as the sign bit.</p> Signup and view all the answers

What happens during unsigned integer expansion when assigning a smaller integer type to a larger one?

<p>The unused bits are zeroed. (D)</p> Signup and view all the answers

In signed integer expansion (sign-extension), the most significant bits are set to zero.

<p>False (B)</p> Signup and view all the answers

What is the term for setting the unused bits to the same value as the sign bit?

<p>sign-extension</p> Signup and view all the answers

When a value from a signed integral type is assigned to an unsigned integral type, the value is ________ as is.

<p>copied</p> Signup and view all the answers

Match the term with its definition:

<p>Unpacking = Translating a value from space-efficient to computation-efficient format. Packing = Translating a value from computation-efficient to space-efficient format.</p> Signup and view all the answers

What is the term used when translating a value from space-efficient to computation-efficient format?

<p>Truncating (C)</p> Signup and view all the answers

Little-endian format unpacks integers from most-significant to least-significant bit.

<p>False (B)</p> Signup and view all the answers

What is the name of the format that unpacks integer most-significant to least-significant bit?

<p>big-endian</p> Signup and view all the answers

In unsigned integer subtraction, if length is zero, then length - 1 equals ______.

<p>4 billion</p> Signup and view all the answers

Which of the following is a typical use case for unsigned integers but NOT for signed integers?

<p>addressing memory locations (C)</p> Signup and view all the answers

In C, if there's a mix of unsigned and signed integers in a single expression, what happens?

<p>The unsigned value is cast to signed. (B)</p> Signup and view all the answers

If -1 < 0U in C, the expression evaluates to true.

<p>False (B)</p> Signup and view all the answers

What value is the integer variable x when it is cast from unsigned integer 4294967295?

<p>-1</p> Signup and view all the answers

When assigning a value from a signed integer type to an unsigned integer type, a negative number is simply ______ as is.

<p>copied</p> Signup and view all the answers

Match the endianness with the bit order:

<p>Little Endian = LSB first Big Endian = MSB first</p> Signup and view all the answers

What is a key consideration for using signed integers when unsigned would otherwise suffice?

<p>They are required for memory addressing. (D)</p> Signup and view all the answers

Interacting with graphics cards never requires manipulation of binary data.

<p>False (B)</p> Signup and view all the answers

What is the C operator used to perform a bitwise OR operation?

<p>|</p> Signup and view all the answers

The result name of function to set the ith bit to 1 of a 64 bit signed value is ___________.

<p>set</p> Signup and view all the answers

When unpacking an integer using little-endian format, which byte is processed first?

<p>A random byte (B)</p> Signup and view all the answers

The following C code results in 55660. Why does this happen?

short x = -9876; unsigned short ux = (unsigned short) x; printf("x = %d, ux = %u\n", x, ux);

<p>The result is undefined bahavior per the C standard (D)</p> Signup and view all the answers

Which of the following accurately describes the role of n in the unpack_signed function?

<p><code>n</code> is used to keep the sign bit positive (C)</p> Signup and view all the answers

In the context of the pack_signed and unpack_signed functions, what does 'src' stand for?

<p>Standardized routing channel (A)</p> Signup and view all the answers

Using twos complement allows CPU to use the same operations code for signed and unsigned integers.

<p>True (A)</p> Signup and view all the answers

What is the decimal representation of 0xff using 8-bit ints?

<p>-1</p> Signup and view all the answers

In C-programming, when assigning an n-bit integral type to m-bit integral type, if n > m, the _______ bits are dropped.

<p>n-m</p> Signup and view all the answers

Given that x is 0x1234cafe, what is the value of z after running the code below?

int x = 0x1234cafe\; char z = x\;

<p>0xfe (D)</p> Signup and view all the answers

Match the C type with the number of bytes in memory:

<p>char = 1 short = 2 int = 4 long = 8</p> Signup and view all the answers

What is one of the key reasons to 'unpack' the integers?

<p>Optimized for maximum flexibility (C)</p> Signup and view all the answers

Java adopted signed/unsigned integers in the style of C/C++ to directly reflect the underlying hardware.

<p>False (B)</p> Signup and view all the answers

What are the values of s and i after the following code has completed?

<p>crash</p> Signup and view all the answers

The bit-pattern of -1 corresponds to ________.

<p>ULONG MAX</p> Signup and view all the answers

Flashcards

Why use Two's Complement?

Integers represented with only one value for zero. The CPU can use the same operations for both signed and unsigned integers.

What is the sizeof operator?

The operator in C used to determine the size of a type or variable in bytes.

What is integer truncation?

Assigning a value from a larger integer type to a smaller one, which may result in losing some data.

What is integer expansion?

Assigning a value from a smaller integer type to a larger one. Unsigned: Unused bits are zeroed. Signed: The unused bits are set to the sign bit

Signup and view all the flashcards

What is Unpacking?

The process of converting a value from space-efficient to computation-efficient format.

Signup and view all the flashcards

What is Packing?

The process of translating a value from computation-efficient to space-efficient format.

Signup and view all the flashcards

What is Little-Endian Unpacking?

A method of unpacking where the least-significant bit is unpacked first.

Signup and view all the flashcards

What is Big-Endian Unpacking?

A method of unpacking where the most-significant bit is unpacked first.

Signup and view all the flashcards

What does set() do?

Sets the i-th bit of a long long integer v to 1.

Signup and view all the flashcards

What does reset() do?

Resets the i-th bit of a long long integer v to 0.

Signup and view all the flashcards

Mixing signed and unsigned takeaways?

If there is a mix of unsigned and signed values in a single expression, signed values are implicitly case to unsigned.

Signup and view all the flashcards

Study Notes

  • In CSCI 1120, integer representation topics included one's complement, two's complement, signed vs. unsigned integers, integer ranges, and addition/multiplication.
  • CSCI 2122 focuses on manipulating integers, covering why systems use two's complement, considerations for mixing signed/unsigned integers, mixing integers of different sizes, and unpacking/packing integers.

Integers in Programming

  • Most languages offer various integer types.
  • Integer sizes in C depend on the platform.
  • char is at least 1 byte.
  • short is at least 2 bytes.
  • int matches the machine's word size.
  • long is at least 4 bytes.
  • long long is at least 8 bytes.
  • Some systems allow int, long, and long long to be the same size.
Type Unsigned Range Unsigned Max Formula Signed Range Signed Range Formula
char 0 to 255 0 to (2^8 - 1) -128 to 127 -2^7 to (2^7 - 1)
short 0 to 65536 0 to (2^16 - 1) -32768 to 32767 -2^15 to (2^15 - 1)
int 0 to 4294967295 0 to (2^32 - 1) -2147483648 to 2147483647 -2^31 to (2^31 - 1)
long 0 to 18446744073709551615 0 to (2^64 - 1) -9223372036854775808 to 9223372036854775807 -2^63 to (2^63 - 1)

Determining Integer Size

  • The sizeof operator determines the size of a type.
  • It takes either a type or a value as input.
  • It returns the number of bytes the type or value occupies.
  • It is used during memory allocation.
  • On Timberlea: char = 1 byte, short = 2 bytes,int = 4 bytes, long = 8 bytes.

Importance of Integer Manipulation

  • Programs must input/output binary data and integers.
  • Programs interact with hardware like graphics cards.
  • Programs translate binary data between different hardware/software.
  • Programs use integers in data structures like bitmaps.
  • Programs implement high-level numerical packages from low-level integers.

Unsigned Integers Review (n-bit integer)

  • Range: 0 to 2^n - 1
  • A value will be b(n-1)2^(n-1) + b(n-2)2^(n-2) +...+b(1)2^1+b(0)2^0 , where bi is the ith bit.
  • Addition uses bitwise operations with carry.
  • Subtraction: converts to addition using the negation of 'y' plus 1: x - y becomes x + ~y + 1.

Two's Complement Examples (8-bit integers)

  • To determine the representation of -1: 1 = 00000001, -1 = ~1 + 1 = ~00000001 + 00000001 = 11111110 + 00000001 = 11111111 = 0xff
  • To determine the representation of -(-1): -1 = 11111111, -(-1) = ~-1 + 1 = ~11111111 + 00000001 = 00000000 + 00000001 = 00000001 = 0x01
  • To determine the representation of -0: 0 = 00000000, -0 = ~0 + 1 = ~00000000 + 00000001 = 11111111 + 00000001 = 00000000 = 0x00
  • To determine the representation of 127 + 1: 127 = 01111111 = 0x7f, 127 + 1 = 01111111 + 00000001 = 10000000 = -128 = 0x80

Why Use Two's Complement?

  • Although, other signed integer representations include one's complement and Signed-Magnitude, two's complement has advantages.
  • The above methods have two representations for 0
  • CPUs can use the same operations for both signed and unsigned integers with two's complement.
  • Operations are the same, except for expansion when copying from a smaller to a larger type.
  • All modern CPUs use two's complement.

Problems

  • Given an n-bit signed integer X where n isn't 8, 16, 32, or 64, determine if X is negative or positive.
  • Without using '<', determine if X < Y and write a C function for this.
  • Write functions for equal, not_equal, less_than_or_equal, greater_than_or_equal, and greater_than.

Casting Integers

  • What happens when assigning an integer from a smaller to a larger type, or vice versa?
  • Also, what results from assigning a signed integer to an unsigned integer or vice versa?

Integer Truncation

  • Assigning a larger integral type value to a smaller integral type may truncate the value.
  • When assigning an n-bit integer to an m-bit integer type where n > m, the n-m most significant bits are dropped and only the 'm' least significant bits are considered.

Integer Expansion (Unsigned)

  • When assigning a value from a smaller integral type to a larger one, the unused bits turn into zeroes.
  • Assigning from an n-bit integer to an m-bit integer where m > n, the 'm-n' most significant bits are 0.

Integer Expansion (Signed)

  • Assigning from a smaller integral type to a larger type, the unused bits are set to the sign bit (sign-extension).
  • Process: Assigning an n-bit integer to an m-bit integer type (m > n), the m-n most significant bits match the sign bit.
  • This is sign-extension.

Integer Conversion

  • Assigning a signed integral type value to an unsigned type simply copies the value.

Casting Surprises: 2'sC to Unsigned

  • Short x = -9876; unsigned short ux = (unsigned short) x;
  • printf("x = %d, ux = %u\n", x, ux); prints x = -9876, ux = ?
  • The bit-pattern of +9876 in 16 bits is 0010 0110 1001 0100.
  • -9876 is 1101 1001 0110 1100.
  • Interpreting -9876's bit-pattern as an unsigned binary gives 55660.

Casting Surprises: Unsigned to 2'sC

  • uint32_t ux = ULONG_MAX;
  • int32_t x = (int) ux;
  • printf("ux = %u, x = %d", ux, x);
  • Prints ux = 4294967295, x = ...

Casting Surprises Takeaways

  • Mixing signed and unsigned values in an expression implicitly casts signed values to unsigned.
  • This includes comparison (<, >, ==, <=, >=).
  • -1 < 0U results in -1 being cast as unsigned.
  • A past slide shows a negative one will have a bit-pattern corresponding to ULONG_MAX
  • 2147483647U > -2147483647-1) RHS casts to unsigned for the bit pattern 1...31zeros... → 2^31
  • Hence the above evaluates to false.
  • 2147483647 > (int) 2147483647U
  • The right-hand side is treated as signed
  • A right Hand side with a bit patter of -1, the above expression evaluates to to True.

Pitfalls of Unsigned Integers

  • Consider the following code:
int sum(int *values, unsigned int length) {
    int s = 0;
    for (int i = 0; i <= length - 1; i++) {
        s += values[i];
    }
    return s;
}
  • If length is 0, length - 1 becomes a large number (4 billion), which can cause a crash.
  • Solution: Use i < length as bound.
unsigned int foo(char *x) { ... }

char *foomax(char *x, char *y) {
    if (foo(x) - foo(y) > 0) {
        return x;
    } else {
        return y;
    }
}
  • The code demonstrates the problems that can occur when using unsigned integers. The expression turns what may intuitively seem negative to unsigned integers, and thus positive.
  • Could write expression foox(x) > foo(y)

Why Not Always Use 2's Complement?

  • Java adopted this approach to avoid the discussed pitfalls.
  • Drawbacks: lose half the integer range when negative numbers aren't needed, many hardware devices depend on unsigned integers (SSDs), need more complex code for signed integers where unsigned are appropriate, and most system integers are unsigned.

Problem: Decomposing an Integer (Little Endian)

void decompose(int x, char *bytes) {
    for (int i = 0; i < 4; i++) {
        bytes[i] = x;
        x = x >> 8;
    }
}
  • The code takes a 4-byte integer and turns the values into its 4 constituent bytes
  • It demonstrates accessing each byte by shifting and assigning.

Application: Unpacking and Packing Integers

  • Integer representation is optimized for speed, not space.
  • E.g. a 64-bit integer with value 1 consumes a lot of space: 00000...0001
  • Space-efficient and Computation efficiency requires unpacking/packing operations.
  • Unpacking: converts a value from space-efficient to computation-efficient format
  • Packing: converts a value from computation-efficient to space-efficient format

Unpacking

  • Unpacking translates from source to destination with the following considerations:
  • Source of n-bit integer where n may not be 8, 16, 32, or 64 bits
  • Destination of m-bit integer with m > n (typical 8, 16, 32, or 64 bits)
  • Check if the integer is signed or unsigned (two's complement).
  • If source is smaller, the integer will need to be expanded
  • If source is larger, the integer will need to be truncated.
  • Little-endian: unpack integer least-significant to most-significant bit.
  • Big-endian: unpack integer most-significant to least-significant bit.

Problem: Unpack an Integer

  • Using an input of long long unpack_signed(char *buffer, int n):
  • Where the parameters: bytes that contain a little Endian value, the amount of bits
  • Write an algorithm to return a long long integer with a signed value
long long unpack_signed(char *buffer, int n) {
    long long result = 0; /* assume positive int */
    if (test(buffer[(n - 1) / 8], (n - 1) % 8)) {
        result = ~result;
    }

    /* assume n <= 64 */
    for (int i = 0; i < n; i++) {
        result = reset(result, i);
        if (test(buffer[i / 8], i % 8)) {
            set(result, i);
        }
    }
    return result;
}

Set and Reset Functions

long long set(long long v, int i) {
    return v | (1 << i);
}

long long reset(long long v, int i) {
    return v & ~(1 << i);
}
  • set() Function:

    • Takes:
      • Long long v: a 64-bit signed value.
      • Integer i: the position of the bit.
    • Returns:
      • The given values with bit i set to 1.
  • reset() Function:

    • Takes:
      • Long long v: a 64-bit signed value.
      • Integer i: the position of the bit.
    • Returns:
      • The given value with bit i set to 0.

Packing

  • Packing: Translates from source to destination.
  • The source is an 'm'-bit integer.
  • The destination is an 'n'-bit integer (n may not be 8, 16, 32, or 64 bits).
  • Considerations consist of weather the integers signed(twos complement)
  • The destination uses little or big endian
int pack_signed(long long src, char *buffer) {
    int n = 63; /* assume full size int */
    int sign = test(src, n);

    for (n = 63; n > 0; n--) {
        if (test(src, n - 1) != sign)
            break;
    }

    for (int i = 0; i <= n; i++) {
        buffer[i >> 3] = reset(buffer[i >> 3], i & 7);
        if (test(src, i)) {
            buffer[i >> 3] = set(buffer[i >> 3], i & 7);
        }
    }

    return n; /* Feature: Need 1 bit to encode 0 or 1 */
}

Problem: Packing an Integer

int pack_signed(long long src, char *buffer)
  • Packing an Integer implementation with the following parameters:
    • src: the value to be packed, (long long) integer that could be signed
    • *buffer: pointer to the bytes containing the packing (little endian)
  • Returns, the number of bits to pack the integer

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

Description

Explore integer representation in computer systems, covering one's complement, two's complement, and signed vs. unsigned integers. Learn about integer ranges, addition/multiplication, and considerations for mixing signed/unsigned integers of different sizes. Understand unpacking and packing integers.

More Like This

Números Racionales y Enteros
5 questions
Mathematics: Integers and Absolute Values
24 questions
Binary Operations and Number Systems Quiz
24 questions
Use Quizgecko on...
Browser
Browser