Lecture 6 Integer Representation and Manipulation
42 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

Which of the following is NOT typically covered when discussing how integers are represented?

  • Two's complement
  • Floating point precision (correct)
  • Integer ranges
  • One's complement

In C, the size of an int is guaranteed to be the same across all platforms.

False (B)

What operator in C is used to determine the size of a data type or variable?

sizeof

_________ is transforming subtraction into addition by the negation of y with 1.

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

Match the data type with its minimum size in C:

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

When assigning a value from a larger integer type to a smaller integer type, what process may occur?

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

Integer expansion of an unsigned integer involves setting the unused bits to the sign bit.

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

What is the name of the process where unused bits are set to the same value as the sign bit during integer expansion?

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

When a value from a signed integer type is assigned to an unsigned integer type, the value is simply _________ as is.

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

In C, if an expression mixes signed and unsigned integers, what typically happens to the signed values?

<p>They are implicitly cast to unsigned. (B)</p> Signup and view all the answers

Java avoids pitfalls in integer manipulation by using the two's complement representation.

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

Give an example of computer hardware where unsigned integers are commonly used.

<p>Hard disks</p> Signup and view all the answers

_________ is translating a value from space-efficient to computation-efficient format.

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

Match the terms with their descriptions in the context of integer manipulation:

<p>Packing = Translating a value from computation-efficient to space-efficient format Unpacking = Translating a value from space-efficient to computation-efficient format Little-endian = Unpack integer least-significant to most-significant bit Big-endian = Unpack integer most-significant to least-significant bit</p> Signup and view all the answers

If you have an n-bit signed integer X (where n is not 8, 16, 32, or 64), how can you determine if X is negative?

<p>Check if the most significant bit (MSB) is set to 1. (C)</p> Signup and view all the answers

Bitwise operations should generally be avoided when trying to optimize the performance of your code.

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

Explain how subtraction can be performed using bitwise operations and addition.

<p>Add the 2's complement</p> Signup and view all the answers

In the context of unpacking integers, _________ refers to unpacking an integer from least-significant bit to most-significant bit.

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

What is the result of the expression 127 + 1 when both numbers are treated as 8-bit signed integers?

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

Integer truncation always results in a loss of information.

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

What is the purpose of 'unpacking' and 'packing' integers?

<p>Efficiently utilizing space</p> Signup and view all the answers

The ______ is a CPU that allows to use same math operation for both signed and unsigned integers due to the way it stores integer.

<p>two's complement</p> Signup and view all the answers

Given the code: char x = -7; short y = x; What will be the value of y?

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

When assigning an int to a char, only the least significant byte of the int is considered; the rest are ignored.

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

In C, the sizeof operator returns the size of a variable or data type in what unit?

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

Using a(n) _________ integer allows the range of numbers to be doubled as there are no negative numbers.

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

How can you unpack a signed integer from a char array in little-endian format?

<p>By reading the least significant byte first and then the most significant byte. (A)</p> Signup and view all the answers

When unpacking an n-bit integer from a source to an m-bit destination where m > n, you only need to consider whether the integer is signed or unsigned, but not the endianness.

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

In unpacking, what does the term 'endianness' refer to?

<p>Byte order</p> Signup and view all the answers

When unpacking a 'big-endian' integer, you must first unpack the _________ to get the proper data.

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

Given that x is -9876, what could the maximum value of ux be unsigned short ux = (unsigned short) x;?

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

Mixing signed and unsigned integers in comparison operations can often lead to incorrect results.

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

Why do most modern systems use two's complement representation for signed integers?

<p>One representation of zero</p> Signup and view all the answers

During __________ , a smaller integral type is assigned to a larger integral type.

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

What happens when a signed char with the value of -2 is assigned to an int?

<p>The int will have the same sign bit. (D)</p> Signup and view all the answers

The sizeof a data type will not change depending on where you run your program.

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

On the Timberlea system mentioned in the content, how many bytes does a short occupy?

<p>2 bytes</p> Signup and view all the answers

The most-significant bit(MSB) in the signed integer tells whether the integer is _________.

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

In general, why is unpacking and packing integers useful?

<p>To save space used by storing values. (C)</p> Signup and view all the answers

For modern systems, it is faster to determine whether 0 > -1 rather than (unsigned int)0 > -1

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

An equivalent of the term unpacking that means the opposite direction would be:

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

Data must flow between a destination that may or may not be of sizes _______.

<p>8, 16, 32, or 64 bits</p> Signup and view all the answers

Flashcards

Integer Representation

Integers are represented using one's complement, two's complement, signed vs unsigned, with specified ranges.

Bit Stream Manipulation

Unpack or pack integers from or to bit streams

int

Is the word size of the machine in C and is platform dependent.

sizeof Operator

Returns the number of bytes a type or value takes up

Signup and view all the flashcards

Unsigned Integer Range

For an n-bit integer, the range is 0 to 2^n-1

Signup and view all the flashcards

Unsigned Integer Addition

Involves a bitwise operation with carry addition

Signup and view all the flashcards

Unsigned Integer Subtraction

Transformed into addition by adding the negation of y with 1

Signup and view all the flashcards

Two's Complement

A method of representing signed integers in computing.

Signup and view all the flashcards

Why Use Two's Complement?

Above two representations have two different representations for 0. The CPU can use the same operations for both signed and unsigned integers if two's complement is used.

Signup and view all the flashcards

Integer Truncation

Assigning an integer from a larger type to a smaller type results in?

Signup and view all the flashcards

Integer Expansion (Unsigned)

Assigning a value from a smaller integer type to a larger integer type, the unused bits are zeroed.

Signup and view all the flashcards

Integer Expansion Sign

Assigning from n-bit integral type to a m-bit integral type, m > n, the m - n most significant bits are set to the same value as the sign bit.

Signup and view all the flashcards

Little-endian

Unpack integers from least-significant to most-significant bit.

Signup and view all the flashcards

Big Endian

Unpack integers from most-significant to least-significant bit

Signup and view all the flashcards

Unpacking

Translating a value from space-efficient to computation-efficient format

Signup and view all the flashcards

Packing

Translating a value from computation-efficient to space-efficient format

Signup and view all the flashcards

set() function

Sets the 'i'th bit in a long long integer v to 1.

Signup and view all the flashcards

reset() function

Sets the 'i'th bit in a long long integer v to 0.

Signup and view all the flashcards

Study Notes

Integer Representation (CSCI 1120)

  • Integers can be represented using one's complement and two's complement
  • Representation types include signed vs unsigned integers
  • Considers representation of integer ranges and addition/multiplication

Integer Manipulation (CSCI 2122)

  • The course discusses how to manipulate integers
  • Systems use two's complement
  • Considers mixing signed and unsigned integers
  • Mixing integers of different sizes requires understanding
  • Discusses how to unpack and pack integers

Lecture Outcomes

  • Ability to unpack/pack integers from/to bit streams
  • Ability to manipulate integer values at the bit level
  • Considerations for mixing signed and unsigned integers are to be identified

Integer Types in C

  • Most languages offer several kinds of integers
  • Integer sizes vary based on the platform in C

Minimum Sizes

  • char is at least 1 byte.
  • short is at least 2 bytes.
  • int is the word size of the machine.
  • long is at least 4 bytes.
  • long long is at least 8 bytes.

Integer Ranges on a system

  • Integer types may be larger than the minimum size on some systems
  • It's possible for int, long, and long long to be the same size
  • Unsigned char range: 0 to 255 or 0 to (2^8 - 1)
  • Signed char range: -128 to 127 or -2^7 to (2^7 - 1)
  • Unsigned short range: 0 to 65536 or 0 to (2^16 - 1)
  • Signed short range: -32768 to 32767 or -2^15 to (2^15 - 1)
  • Unsigned int range: 0 to 4294967295 or 0 to (2^32 - 1)
  • Signed int range: -2147483648 to 2147483647 or -2^31 to (2^31 - 1)
  • Unsigned long range: 0 to 18446744073709551615 or 0 to (2^64 - 1)
  • Signed long range: -9223372036854775808 to 9223372036854775807 or -2^63 to (2^63 - 1)

Determining Integer Size

  • Applications may need to know the size of an integer

sizeof Operator

  • Used to determine the size of a type
  • Takes a type or a value
  • Returns the number of bytes the type or value occupies
  • It's used extensively when allocating memory
  • char size: 1 byte
  • short size: 2 bytes
  • int size: 4 bytes
  • long size: 8 bytes

Importance of Integer Manipulation

  • Essential for programs to input and write binary data, including integers
  • Important for interacting with hardware, like graphics cards
  • Critical for translating binary data between different hardware and software
  • Necessary when using integers as components of data structures, like bitmaps
  • It's required to implement high-level numerical packages from integers at a low level

Unsigned Integers

  • For an n-bit integer, the range is 0 to 2^n - 1
  • Value is calculated as: bn-1 * 2^(n-1) + bn-2 * 2^(n-2) + ... + b1 * 2^1 + b0 * 2^0 where bi is the i-th bit in the int
  • Addition operates bitwise with carry
  • Subtraction is transformed into addition by adding the negation of y with 1 (x - y -> x + ~y + 1)

Two's Complement Examples (8-bit)

  • -1 is represented: 1 = 00000001, -1 = (~1 + 1) = ~00000001 + 00000001 = 11111110 + 00000001 = 11111111 = 0xff
  • -(-1) is represented: -1 = 11111111, -(-1) = (~-1 + 1) = ~11111111 + 00000001 = 00000000 + 00000001 = 00000001 = 0x01
  • -0 is represented: 0 = 00000000, -0 = (~0 + 1) = ~00000000 + 00000001 = 11111111 + 00000001 = 00000000 = 0x00
  • 127 + 1 is represented: 127 = 01111111 = 0x7f, 127 + 1 = 01111111 + 00000001 = 10000000 = -128 = 0x80

Usage of Two's Complement

  • Signed integer representations, such as one's complement and signed-magnitude, are alternatives
  • Two's complement is used because other representations have two representations for 0
  • The CPU can use the same operations for both signed and unsigned integers if two's complement is used
  • For two's complement all operations are the same, except expansion (copying an integer value from a smaller integral type to a larger one)
  • All modern CPUs use two's complement

Problems with Signed Integers

  • Tasks includes how to determine if an n-bit signed integer X (where n is not 8, 16, 32, or 64) is negative or positive
  • Write a C function to determine if X < Y (without using <)

Integer Casting

  • Occurs when assigning an integer between smaller/larger integral types
  • When assigning a value from a larger integral type to a smaller one, the value may be truncated

Integer Truncation

  • Values assigned from a larger integral type to a smaller one may be truncated
  • When assigning an n-bit integral type to an m-bit integral type where n > m, the n-m most significant bits are dropped and only the m least significant bits are considered

Integer Expansion: Unsigned

  • Assigned from a smaller integral type to a larger integral type, the unused bits are zeroed
  • When assigning from an n-bit integral type to a m-bit integral type, where m > n, the m-n most significant bits are 0

Integer Expansion: Signed

  • Assigned from a smaller integral type to a larger integral type, the unused bits are set to the sign bit (sign-extended)
  • When assigning from an n-bit to an m-bit integral type, with m > n, the m-n most significant bits are set to the same value as the sign bit
  • This is called sign-extension

Integer Conversion

  • Assigned from a signed integral type to an unsigned integral type, the value is simply copied as is

Pitfalls of Casting

  • Evaluating the bit-pattern of the int -9876 as Unsigned Binary gives: → 55660
  • With an unsigned to 2sC prints ux = 4294967295, x = some value with a bit pattern

Implicit Casting

  • Signed values are implicitly cast to unsigned in single expression when mixing them
  • Includes comparison operators

Potential Issues With Unsigned Comparisons

  • Comparison to 0U means -1 has the bit-pattern corresponding to ULONG_MAX, so the comparison is false
  • Comparing 2147483647U > -2147483647 - 1 means the right-hand side is implicitly cast as unsigned, resulting in a false comparison
  • However, 2147483647 > (int) 2147483647U treats the right-hand side as signed (-1), leading to a true comparison

Handling Unsigned and Negative Numbers

  • When using unsigned ints it can create bugs if not handled carefully
  • When mixing signed and unsigned values in comparisons, unexpected results may arise because of implicit casting (e.g., i <= length - 1)

Avoiding Issues from Signedness

  • Java chose to use only signed integers to avoid these pitfalls

Downside to Signed Only

  • Half the range of integers may be lost if negative numbers are not needed
  • Many hardware devices use unsigned integers (e.g., hard disks and SSDs)
  • More complex code may be needed to use signed integers where unsigned would be appropriate otherwise
  • Most integers in systems are unsigned

Decomposing Integers

  • Given a 4-byte integer, it can be decomposed into its 4 bytes in little-endian order

Application for Integer Packing

  • Integer representation is optimized for speed during execution, not space
  • A 64-bit integer containing the value 1 takes a lot of space

Efficient Space Allocation

  • Many applications require more efficient use of space
  • Include communication protocols, device interfaces, and database format
  • Space-efficient and computation-efficient formats necessitates efficient conversion for use

Unpacking

  • Translates a value from a space-efficient to a computation-efficient format

Packing

  • Translates a value from a computation-efficient to a space-efficient format

Unpacking Process

  • Process translates from source to destination
  • Considers a source as an n-bit integer (n may not be 8, 16, 32, or 64 bits)
  • The end point is an m-bit integer, where m > n (if possible) and it is typically 8, 16, 32, or 64

Unpacking Considerations

  • Integer can be unsigned or signed (typically two’s complement)
  • If the source larger or smaller than the destination
  • Integer will need to be expanded if smaller
  • Integer will need to be truncated if larger
  • the source is little endian or big endian has to be checked
  • With Little-endian unpack integer least-significant to most-significant bit
  • With Big-endian unpack integer most-significant to least-significant bit

Unpacking Function

  • Implementing unpacking function with Parameters includes pointer to the bytes containing value and n is the number of bits
  • It returns a long long containing integer.
  • The approach includes initializing result to the appropriate sign extension and copying over the bits from least significant to most significant.

SET() and RESET() Methods

  • Sets a specified bit
  • Takes a long long v (64 bit signed value) and integer i

Set Implementation

  • Implemented with the code return v | (1

The Packing Function

  • To implement the packing function Parameters needs to be passed which is integer to be packed, and bytes containing packing
  • Check to see if bytes has little endian
  • Then the number of bits required to pack the integer is returned

Determine the Packed Integer

  • Determine size of integer by reducing sign extension to the minimum 0000000000000000000000000000000000000000000000000000000000000001
  • Copy over the bits from least significant to most significant

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 using one's complement and two's complement. Learn to manipulate integers, understanding signed vs. unsigned types and different sizes in C. Master bit-level operations and packing/unpacking.

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